]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Impl.c
refine the code and add more security check.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Impl.c
1 /** @file
2
3 Copyright (c) 2005 - 2010, Intel Corporation.<BR>
4 All rights reserved. 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 EFI_IPSEC_PROTOCOL *mIpSec = NULL;
17
18 /**
19 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.
20
21 The GetModeData() function returns the current operational mode data for this
22 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This
23 function is used optionally to retrieve the operational mode data of underlying
24 networks or drivers.
25
26 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
27 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
28 @param[out] MnpConfigData Pointer to the managed network configuration data structure.
29 @param[out] SnpModeData Pointer to the simple network mode data structure.
30
31 @retval EFI_SUCCESS The operation completed successfully.
32 @retval EFI_INVALID_PARAMETER This is NULL.
33 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 EfiIp4GetModeData (
39 IN CONST EFI_IP4_PROTOCOL *This,
40 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
41 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
42 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
43 );
44
45 /**
46 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.
47
48 The Configure() function is used to set, change, or reset the operational
49 parameters and filter settings for this EFI IPv4 Protocol instance. Until these
50 parameters have been set, no network traffic can be sent or received by this
51 instance. Once the parameters have been reset (by calling this function with
52 IpConfigData set to NULL), no more traffic can be sent or received until these
53 parameters have been set again. Each EFI IPv4 Protocol instance can be started
54 and stopped independently of each other by enabling or disabling their receive
55 filter settings with the Configure() function.
56
57 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will
58 be appended as an alias address into the addresses list in the EFI IPv4 Protocol
59 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL
60 to retrieve the default IPv4 address if it is not available yet. Clients could
61 frequently call GetModeData() to check the status to ensure that the default IPv4
62 address is ready.
63
64 If operational parameters are reset or changed, any pending transmit and receive
65 requests will be cancelled. Their completion token status will be set to EFI_ABORTED
66 and their events will be signaled.
67
68 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
69 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.
70
71 @retval EFI_SUCCESS The driver instance was successfully opened.
72 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
73 RARP, etc.) is not finished yet.
74 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
75 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:
76 A configuration protocol (DHCP, BOOTP, RARP, etc.) could
77 not be located when clients choose to use the default IPv4
78 address. This EFI IPv4 Protocol implementation does not
79 support this requested filter or timeout setting.
80 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.
81 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the
82 IPv4 address or subnet mask can be changed. The interface must
83 also be stopped when switching to/from raw packet mode.
84 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4
85 Protocol driver instance is not opened.
86
87 **/
88 EFI_STATUS
89 EFIAPI
90 EfiIp4Configure (
91 IN EFI_IP4_PROTOCOL *This,
92 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL
93 );
94
95 /**
96 Joins and leaves multicast groups.
97
98 The Groups() function is used to join and leave multicast group sessions. Joining
99 a group will enable reception of matching multicast packets. Leaving a group will
100 disable the multicast packet reception.
101
102 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
103
104 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
105 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.
106 @param[in] GroupAddress Pointer to the IPv4 multicast address.
107
108 @retval EFI_SUCCESS The operation completed successfully.
109 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
110 - This is NULL.
111 - JoinFlag is TRUE and GroupAddress is NULL.
112 - GroupAddress is not NULL and *GroupAddress is
113 not a multicast IPv4 address.
114 @retval EFI_NOT_STARTED This instance has not been started.
115 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
116 RARP, etc.) is not finished yet.
117 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
118 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.
119 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
120 JoinFlag is TRUE).
121 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
122 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
123
124 **/
125 EFI_STATUS
126 EFIAPI
127 EfiIp4Groups (
128 IN EFI_IP4_PROTOCOL *This,
129 IN BOOLEAN JoinFlag,
130 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
131 );
132
133 /**
134 Adds and deletes routing table entries.
135
136 The Routes() function adds a route to or deletes a route from the routing table.
137
138 Routes are determined by comparing the SubnetAddress with the destination IPv4
139 address arithmetically AND-ed with the SubnetMask. The gateway address must be
140 on the same subnet as the configured station address.
141
142 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
143 The default route matches all destination IPv4 addresses that do not match any
144 other routes.
145
146 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination
147 IP address if it can be found in the ARP cache or on the local subnet. One automatic
148 nonroute entry will be inserted into the routing table for outgoing packets that
149 are addressed to a local subnet (gateway address of 0.0.0.0).
150
151 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI
152 IPv4 Protocol instances that use the default IPv4 address will also have copies
153 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these
154 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its
155 instances. As a result, client modification to the routing table will be lost.
156
157 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
158 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
159 FALSE to add this route to the routing table. SubnetAddress
160 and SubnetMask are used as the key to each route entry.
161 @param[in] SubnetAddress The address of the subnet that needs to be routed.
162 @param[in] SubnetMask The subnet mask of SubnetAddress.
163 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.
164
165 @retval EFI_SUCCESS The operation completed successfully.
166 @retval EFI_NOT_STARTED The driver instance has not been started.
167 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
168 RARP, etc.) is not finished yet.
169 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
170 - This is NULL.
171 - SubnetAddress is NULL.
172 - SubnetMask is NULL.
173 - GatewayAddress is NULL.
174 - *SubnetAddress is not a valid subnet address.
175 - *SubnetMask is not a valid subnet mask.
176 - *GatewayAddress is not a valid unicast IPv4 address.
177 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
178 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
179 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
180 DeleteRoute is FALSE).
181
182 **/
183 EFI_STATUS
184 EFIAPI
185 EfiIp4Routes (
186 IN EFI_IP4_PROTOCOL *This,
187 IN BOOLEAN DeleteRoute,
188 IN EFI_IPv4_ADDRESS *SubnetAddress,
189 IN EFI_IPv4_ADDRESS *SubnetMask,
190 IN EFI_IPv4_ADDRESS *GatewayAddress
191 );
192
193 /**
194 Places outgoing data packets into the transmit queue.
195
196 The Transmit() function places a sending request in the transmit queue of this
197 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some
198 errors occur, the event in the token will be signaled and the status is updated.
199
200 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
201 @param[in] Token Pointer to the transmit token.
202
203 @retval EFI_SUCCESS The data has been queued for transmission.
204 @retval EFI_NOT_STARTED This instance has not been started.
205 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
206 RARP, etc.) is not finished yet.
207 @retval EFI_INVALID_PARAMETER One or more pameters are invalid.
208 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event
209 was already in the transmit queue.
210 @retval EFI_NOT_READY The completion token could not be queued because the transmit
211 queue is full.
212 @retval EFI_NOT_FOUND Not route is found to destination address.
213 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
214 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
215 short to transmit.
216 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is
217 greater than MTU (or greater than the maximum packet size if
218 Token.Packet.TxData.OverrideData.
219 DoNotFragment is TRUE.)
220
221 **/
222 EFI_STATUS
223 EFIAPI
224 EfiIp4Transmit (
225 IN EFI_IP4_PROTOCOL *This,
226 IN EFI_IP4_COMPLETION_TOKEN *Token
227 );
228
229 /**
230 Places a receiving request into the receiving queue.
231
232 The Receive() function places a completion token into the receive packet queue.
233 This function is always asynchronous.
234
235 The Token.Event field in the completion token must be filled in by the caller
236 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol
237 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
238 is signaled.
239
240 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
241 @param[in] Token Pointer to a token that is associated with the receive data descriptor.
242
243 @retval EFI_SUCCESS The receive completion token was cached.
244 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
245 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)
246 is not finished yet.
247 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
248 - This is NULL.
249 - Token is NULL.
250 - Token.Event is NULL.
251 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
252 resources (usually memory).
253 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
254 The EFI IPv4 Protocol instance has been reset to startup defaults.
255 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
256 in the receive queue.
257 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
258 @retval EFI_ICMP_ERROR An ICMP error packet was received.
259
260 **/
261 EFI_STATUS
262 EFIAPI
263 EfiIp4Receive (
264 IN EFI_IP4_PROTOCOL *This,
265 IN EFI_IP4_COMPLETION_TOKEN *Token
266 );
267
268 /**
269 Abort an asynchronous transmit or receive request.
270
271 The Cancel() function is used to abort a pending transmit or receive request.
272 If the token is in the transmit or receive request queues, after calling this
273 function, Token->Status will be set to EFI_ABORTED and then Token->Event will
274 be signaled. If the token is not in one of the queues, which usually means the
275 asynchronous operation has completed, this function will not signal the token
276 and EFI_NOT_FOUND is returned.
277
278 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
279 @param[in] Token Pointer to a token that has been issued by
280 EFI_IP4_PROTOCOL.Transmit() or
281 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending
282 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is
283 defined in EFI_IP4_PROTOCOL.Transmit().
284
285 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
286 Token.->Event was signaled. When Token is NULL, all
287 pending requests were aborted and their events were signaled.
288 @retval EFI_INVALID_PARAMETER This is NULL.
289 @retval EFI_NOT_STARTED This instance has not been started.
290 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
291 RARP, etc.) is not finished yet.
292 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
293 not found in the transmit or receive queue. It has either completed
294 or was not issued by Transmit() and Receive().
295
296 **/
297 EFI_STATUS
298 EFIAPI
299 EfiIp4Cancel (
300 IN EFI_IP4_PROTOCOL *This,
301 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
302 );
303
304 /**
305 Polls for incoming data packets and processes outgoing data packets.
306
307 The Poll() function polls for incoming data packets and processes outgoing data
308 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()
309 function to increase the rate that data packets are moved between the communications
310 device and the transmit and receive queues.
311
312 In some systems the periodic timer event may not poll the underlying communications
313 device fast enough to transmit and/or receive all data packets without missing
314 incoming packets or dropping outgoing packets. Drivers and applications that are
315 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function
316 more often.
317
318 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
319
320 @retval EFI_SUCCESS Incoming or outgoing data was processed.
321 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
322 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
323 RARP, etc.) is not finished yet.
324 @retval EFI_INVALID_PARAMETER This is NULL.
325 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
326 @retval EFI_NOT_READY No incoming or outgoing data is processed.
327 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
328 Consider increasing the polling rate.
329
330 **/
331 EFI_STATUS
332 EFIAPI
333 EfiIp4Poll (
334 IN EFI_IP4_PROTOCOL *This
335 );
336
337 EFI_IP4_PROTOCOL
338 mEfiIp4ProtocolTemplete = {
339 EfiIp4GetModeData,
340 EfiIp4Configure,
341 EfiIp4Groups,
342 EfiIp4Routes,
343 EfiIp4Transmit,
344 EfiIp4Receive,
345 EfiIp4Cancel,
346 EfiIp4Poll
347 };
348
349 /**
350 Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.
351
352 The GetModeData() function returns the current operational mode data for this
353 driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This
354 function is used optionally to retrieve the operational mode data of underlying
355 networks or drivers.
356
357 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
358 @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
359 @param[out] MnpConfigData Pointer to the managed network configuration data structure.
360 @param[out] SnpModeData Pointer to the simple network mode data structure.
361
362 @retval EFI_SUCCESS The operation completed successfully.
363 @retval EFI_INVALID_PARAMETER This is NULL.
364 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
365
366 **/
367 EFI_STATUS
368 EFIAPI
369 EfiIp4GetModeData (
370 IN CONST EFI_IP4_PROTOCOL *This,
371 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
372 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
373 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
374 )
375 {
376 IP4_PROTOCOL *IpInstance;
377 IP4_SERVICE *IpSb;
378 EFI_IP4_CONFIG_DATA *Config;
379 EFI_STATUS Status;
380 EFI_TPL OldTpl;
381 IP4_ADDR Ip;
382
383 if (This == NULL) {
384 return EFI_INVALID_PARAMETER;
385 }
386
387 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
388 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
389 IpSb = IpInstance->Service;
390
391 if (Ip4ModeData != NULL) {
392 //
393 // IsStarted is "whether the EfiIp4Configure has been called".
394 // IsConfigured is "whether the station address has been configured"
395 //
396 Ip4ModeData->IsStarted = (BOOLEAN)(IpInstance->State == IP4_STATE_CONFIGED);
397 CopyMem (&Ip4ModeData->ConfigData, &IpInstance->ConfigData, sizeof (Ip4ModeData->ConfigData));
398 Ip4ModeData->IsConfigured = FALSE;
399
400 Ip4ModeData->GroupCount = IpInstance->GroupCount;
401 Ip4ModeData->GroupTable = (EFI_IPv4_ADDRESS *) IpInstance->Groups;
402
403 Ip4ModeData->IcmpTypeCount = 23;
404 Ip4ModeData->IcmpTypeList = mIp4SupportedIcmp;
405
406 Ip4ModeData->RouteTable = NULL;
407 Ip4ModeData->RouteCount = 0;
408
409 Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize;
410
411 //
412 // return the current station address for this IP child. So,
413 // the user can get the default address through this. Some
414 // application wants to know it station address even it is
415 // using the default one, such as a ftp server.
416 //
417 if (Ip4ModeData->IsStarted) {
418 Config = &Ip4ModeData->ConfigData;
419
420 Ip = HTONL (IpInstance->Interface->Ip);
421 CopyMem (&Config->StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
422
423 Ip = HTONL (IpInstance->Interface->SubnetMask);
424 CopyMem (&Config->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));
425
426 Ip4ModeData->IsConfigured = IpInstance->Interface->Configured;
427
428 //
429 // Build a EFI route table for user from the internal route table.
430 //
431 Status = Ip4BuildEfiRouteTable (IpInstance);
432
433 if (EFI_ERROR (Status)) {
434 gBS->RestoreTPL (OldTpl);
435 return Status;
436 }
437
438 Ip4ModeData->RouteTable = IpInstance->EfiRouteTable;
439 Ip4ModeData->RouteCount = IpInstance->EfiRouteCount;
440 }
441 }
442
443 if (MnpConfigData != NULL) {
444 CopyMem (MnpConfigData, &IpSb->MnpConfigData, sizeof (*MnpConfigData));
445 }
446
447 if (SnpModeData != NULL) {
448 CopyMem (SnpModeData, &IpSb->SnpMode, sizeof (*SnpModeData));
449 }
450
451 gBS->RestoreTPL (OldTpl);
452 return EFI_SUCCESS;
453 }
454
455
456 /**
457 Config the MNP parameter used by IP. The IP driver use one MNP
458 child to transmit/receive frames. By default, it configures MNP
459 to receive unicast/multicast/broadcast. And it will enable/disable
460 the promiscous receive according to whether there is IP child
461 enable that or not. If Force is FALSE, it will iterate through
462 all the IP children to check whether the promiscuous receive
463 setting has been changed. If it hasn't been changed, it won't
464 reconfigure the MNP. If Force is TRUE, the MNP is configured no
465 matter whether that is changed or not.
466
467 @param[in] IpSb The IP4 service instance that is to be changed.
468 @param[in] Force Force the configuration or not.
469
470 @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.
471 @retval Others Configuration failed.
472
473 **/
474 EFI_STATUS
475 Ip4ServiceConfigMnp (
476 IN IP4_SERVICE *IpSb,
477 IN BOOLEAN Force
478 )
479 {
480 LIST_ENTRY *Entry;
481 LIST_ENTRY *ProtoEntry;
482 IP4_INTERFACE *IpIf;
483 IP4_PROTOCOL *IpInstance;
484 BOOLEAN Reconfig;
485 BOOLEAN PromiscReceive;
486 EFI_STATUS Status;
487
488 Reconfig = FALSE;
489 PromiscReceive = FALSE;
490
491 if (!Force) {
492 //
493 // Iterate through the IP children to check whether promiscuous
494 // receive setting has been changed. Update the interface's receive
495 // filter also.
496 //
497 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
498
499 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
500 IpIf->PromiscRecv = FALSE;
501
502 NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) {
503 IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP4_PROTOCOL, AddrLink);
504
505 if (IpInstance->ConfigData.AcceptPromiscuous) {
506 IpIf->PromiscRecv = TRUE;
507 PromiscReceive = TRUE;
508 }
509 }
510 }
511
512 //
513 // If promiscuous receive isn't changed, it isn't necessary to reconfigure.
514 //
515 if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) {
516 return EFI_SUCCESS;
517 }
518
519 Reconfig = TRUE;
520 IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive;
521 }
522
523 Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData);
524
525 //
526 // recover the original configuration if failed to set the configure.
527 //
528 if (EFI_ERROR (Status) && Reconfig) {
529 IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive;
530 }
531
532 return Status;
533 }
534
535
536 /**
537 The event handle for IP4 auto configuration. If IP is asked
538 to reconfigure the default address. The original default
539 interface and route table are removed as the default. If there
540 is active IP children using the default address, the interface
541 will remain valid until all the children have freed their
542 references. If IP is signalled when auto configuration is done,
543 it will configure the default interface and default route table
544 with the configuration information retrieved by IP4_CONFIGURE.
545
546 @param[in] Context The IP4 service binding instance.
547
548 **/
549 VOID
550 EFIAPI
551 Ip4AutoConfigCallBackDpc (
552 IN VOID *Context
553 )
554 {
555 EFI_IP4_CONFIG_PROTOCOL *Ip4Config;
556 EFI_IP4_IPCONFIG_DATA *Data;
557 EFI_IP4_ROUTE_TABLE *RouteEntry;
558 IP4_SERVICE *IpSb;
559 IP4_ROUTE_TABLE *RouteTable;
560 IP4_INTERFACE *IpIf;
561 EFI_STATUS Status;
562 UINTN Len;
563 UINT32 Index;
564 IP4_ADDR StationAddress;
565 IP4_ADDR SubnetMask;
566 IP4_ADDR SubnetAddress;
567 IP4_ADDR GatewayAddress;
568
569 IpSb = (IP4_SERVICE *) Context;
570 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);
571
572 Ip4Config = IpSb->Ip4Config;
573
574 //
575 // IP is asked to do the reconfiguration. If the default interface
576 // has been configured, release the default interface and route
577 // table, then create a new one. If there are some IP children
578 // using it, the interface won't be physically freed until all the
579 // children have released their reference to it. Also remember to
580 // restart the receive on the default address. IP4 driver only receive
581 // frames on the default address, and when the default interface is
582 // freed, Ip4AcceptFrame won't be informed.
583 //
584 if (IpSb->ActiveEvent == IpSb->ReconfigEvent) {
585
586 if (IpSb->DefaultInterface->Configured) {
587 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
588
589 if (IpIf == NULL) {
590 return;
591 }
592
593 RouteTable = Ip4CreateRouteTable ();
594
595 if (RouteTable == NULL) {
596 Ip4FreeInterface (IpIf, NULL);
597 return;
598 }
599
600 Ip4CancelReceive (IpSb->DefaultInterface);
601 Ip4FreeInterface (IpSb->DefaultInterface, NULL);
602 Ip4FreeRouteTable (IpSb->DefaultRouteTable);
603
604 IpSb->DefaultInterface = IpIf;
605 InsertHeadList (&IpSb->Interfaces, &IpIf->Link);
606
607 IpSb->DefaultRouteTable = RouteTable;
608 Ip4ReceiveFrame (IpIf, NULL, Ip4AccpetFrame, IpSb);
609 }
610
611 Ip4Config->Stop (Ip4Config);
612 Ip4Config->Start (Ip4Config, IpSb->DoneEvent, IpSb->ReconfigEvent);
613 return ;
614 }
615
616 //
617 // Get the configure data in two steps: get the length then the data.
618 //
619 Len = 0;
620
621 if (Ip4Config->GetData (Ip4Config, &Len, NULL) != EFI_BUFFER_TOO_SMALL) {
622 return ;
623 }
624
625 Data = AllocatePool (Len);
626
627 if (Data == NULL) {
628 return ;
629 }
630
631 Status = Ip4Config->GetData (Ip4Config, &Len, Data);
632
633 if (EFI_ERROR (Status)) {
634 goto ON_EXIT;
635 }
636
637 IpIf = IpSb->DefaultInterface;
638
639 //
640 // If the default address has been configured don't change it.
641 // This is unlikely to happen if EFI_IP4_CONFIG protocol has
642 // informed us to reconfigure each time it wants to change the
643 // configuration parameters.
644 //
645 if (IpIf->Configured) {
646 goto ON_EXIT;
647 }
648
649 //
650 // Set the default interface's address, then add a directed
651 // route for it, that is, the route whose nexthop is zero.
652 //
653 StationAddress = EFI_NTOHL (Data->StationAddress);
654 SubnetMask = EFI_NTOHL (Data->SubnetMask);
655 Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask);
656
657 if (EFI_ERROR (Status)) {
658 goto ON_EXIT;
659 }
660
661 Ip4AddRoute (
662 IpSb->DefaultRouteTable,
663 StationAddress,
664 SubnetMask,
665 IP4_ALLZERO_ADDRESS
666 );
667
668 //
669 // Add routes returned by EFI_IP4_CONFIG protocol.
670 //
671 for (Index = 0; Index < Data->RouteTableSize; Index++) {
672 RouteEntry = &Data->RouteTable[Index];
673
674 SubnetAddress = EFI_NTOHL (RouteEntry->SubnetAddress);
675 SubnetMask = EFI_NTOHL (RouteEntry->SubnetMask);
676 GatewayAddress = EFI_NTOHL (RouteEntry->GatewayAddress);
677 Ip4AddRoute (IpSb->DefaultRouteTable, SubnetAddress, SubnetMask, GatewayAddress);
678 }
679
680 IpSb->State = IP4_SERVICE_CONFIGED;
681
682 Ip4SetVariableData (IpSb);
683
684 ON_EXIT:
685 FreePool (Data);
686 }
687
688 /**
689 Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK.
690
691 @param Event The event that is signalled.
692 @param Context The IP4 service binding instance.
693
694 **/
695 VOID
696 EFIAPI
697 Ip4AutoConfigCallBack (
698 IN EFI_EVENT Event,
699 IN VOID *Context
700 )
701 {
702 IP4_SERVICE *IpSb;
703
704 IpSb = (IP4_SERVICE *) Context;
705 IpSb->ActiveEvent = Event;
706
707 //
708 // Request Ip4AutoConfigCallBackDpc as a DPC at TPL_CALLBACK
709 //
710 QueueDpc (TPL_CALLBACK, Ip4AutoConfigCallBackDpc, Context);
711 }
712
713
714 /**
715 Start the auto configuration for this IP service instance.
716 It will locates the EFI_IP4_CONFIG_PROTOCOL, then start the
717 auto configuration.
718
719 @param[in] IpSb The IP4 service instance to configure
720
721 @retval EFI_SUCCESS The auto configuration is successfull started
722 @retval Others Failed to start auto configuration.
723
724 **/
725 EFI_STATUS
726 Ip4StartAutoConfig (
727 IN IP4_SERVICE *IpSb
728 )
729 {
730 EFI_IP4_CONFIG_PROTOCOL *Ip4Config;
731 EFI_STATUS Status;
732
733 if (IpSb->State > IP4_SERVICE_UNSTARTED) {
734 return EFI_SUCCESS;
735 }
736
737 //
738 // Create the DoneEvent and ReconfigEvent to call EFI_IP4_CONFIG
739 //
740 Status = gBS->CreateEvent (
741 EVT_NOTIFY_SIGNAL,
742 TPL_CALLBACK,
743 Ip4AutoConfigCallBack,
744 IpSb,
745 &IpSb->DoneEvent
746 );
747
748 if (EFI_ERROR (Status)) {
749 return Status;
750 }
751
752 Status = gBS->CreateEvent (
753 EVT_NOTIFY_SIGNAL,
754 TPL_NOTIFY,
755 Ip4AutoConfigCallBack,
756 IpSb,
757 &IpSb->ReconfigEvent
758 );
759
760 if (EFI_ERROR (Status)) {
761 goto CLOSE_DONE_EVENT;
762 }
763
764 //
765 // Open the EFI_IP4_CONFIG protocol then start auto configure
766 //
767 Status = gBS->OpenProtocol (
768 IpSb->Controller,
769 &gEfiIp4ConfigProtocolGuid,
770 (VOID **) &Ip4Config,
771 IpSb->Image,
772 IpSb->Controller,
773 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
774 );
775
776 if (EFI_ERROR (Status)) {
777 Status = EFI_UNSUPPORTED;
778 goto CLOSE_RECONFIG_EVENT;
779 }
780
781 Status = Ip4Config->Start (Ip4Config, IpSb->DoneEvent, IpSb->ReconfigEvent);
782
783 if (EFI_ERROR (Status)) {
784 gBS->CloseProtocol (
785 IpSb->Controller,
786 &gEfiIp4ConfigProtocolGuid,
787 IpSb->Image,
788 IpSb->Controller
789 );
790
791 goto CLOSE_RECONFIG_EVENT;
792 }
793
794 IpSb->Ip4Config = Ip4Config;
795 IpSb->State = IP4_SERVICE_STARTED;
796 return Status;
797
798 CLOSE_RECONFIG_EVENT:
799 gBS->CloseEvent (IpSb->ReconfigEvent);
800 IpSb->ReconfigEvent = NULL;
801
802 CLOSE_DONE_EVENT:
803 gBS->CloseEvent (IpSb->DoneEvent);
804 IpSb->DoneEvent = NULL;
805
806 return Status;
807 }
808
809
810 /**
811 Intiialize the IP4_PROTOCOL structure to the unconfigured states.
812
813 @param IpSb The IP4 service instance.
814 @param IpInstance The IP4 child instance.
815
816 **/
817 VOID
818 Ip4InitProtocol (
819 IN IP4_SERVICE *IpSb,
820 IN OUT IP4_PROTOCOL *IpInstance
821 )
822 {
823 ASSERT ((IpSb != NULL) && (IpInstance != NULL));
824
825 ZeroMem (IpInstance, sizeof (IP4_PROTOCOL));
826
827 IpInstance->Signature = IP4_PROTOCOL_SIGNATURE;
828 CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto));
829 IpInstance->State = IP4_STATE_UNCONFIGED;
830 IpInstance->Service = IpSb;
831
832 InitializeListHead (&IpInstance->Link);
833 NetMapInit (&IpInstance->RxTokens);
834 NetMapInit (&IpInstance->TxTokens);
835 InitializeListHead (&IpInstance->Received);
836 InitializeListHead (&IpInstance->Delivered);
837 InitializeListHead (&IpInstance->AddrLink);
838
839 EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);
840 }
841
842
843 /**
844 Configure the IP4 child. If the child is already configured,
845 change the configuration parameter. Otherwise configure it
846 for the first time. The caller should validate the configuration
847 before deliver them to it. It also don't do configure NULL.
848
849 @param[in, out] IpInstance The IP4 child to configure.
850 @param[in] Config The configure data.
851
852 @retval EFI_SUCCESS The IP4 child is successfully configured.
853 @retval EFI_DEVICE_ERROR Failed to free the pending transive or to
854 configure underlying MNP or other errors.
855 @retval EFI_NO_MAPPING The IP4 child is configured to use default
856 address, but the default address hasn't been
857 configured. The IP4 child doesn't need to be
858 reconfigured when default address is configured.
859 @retval EFI_OUT_OF_RESOURCES No more memory space is available.
860 @retval other Other error occurs.
861
862 **/
863 EFI_STATUS
864 Ip4ConfigProtocol (
865 IN OUT IP4_PROTOCOL *IpInstance,
866 IN EFI_IP4_CONFIG_DATA *Config
867 )
868 {
869 IP4_SERVICE *IpSb;
870 IP4_INTERFACE *IpIf;
871 EFI_STATUS Status;
872 IP4_ADDR Ip;
873 IP4_ADDR Netmask;
874
875 IpSb = IpInstance->Service;
876
877 //
878 // User is changing packet filters. It must be stopped
879 // before the station address can be changed.
880 //
881 if (IpInstance->State == IP4_STATE_CONFIGED) {
882 //
883 // Cancel all the pending transmit/receive from upper layer
884 //
885 Status = Ip4Cancel (IpInstance, NULL);
886
887 if (EFI_ERROR (Status)) {
888 return EFI_DEVICE_ERROR;
889 }
890
891 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));
892 return EFI_SUCCESS;
893 }
894
895 //
896 // Configure a fresh IP4 protocol instance. Create a route table.
897 // Each IP child has its own route table, which may point to the
898 // default table if it is using default address.
899 //
900 Status = EFI_OUT_OF_RESOURCES;
901 IpInstance->RouteTable = Ip4CreateRouteTable ();
902
903 if (IpInstance->RouteTable == NULL) {
904 return Status;
905 }
906
907 //
908 // Set up the interface.
909 //
910 CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR));
911 CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));
912
913 Ip = NTOHL (Ip);
914 Netmask = NTOHL (Netmask);
915
916 if (!Config->UseDefaultAddress) {
917 //
918 // Find whether there is already an interface with the same
919 // station address. All the instances with the same station
920 // address shares one interface.
921 //
922 IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask);
923
924 if (IpIf != NULL) {
925 NET_GET_REF (IpIf);
926
927 } else {
928 IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
929
930 if (IpIf == NULL) {
931 goto ON_ERROR;
932 }
933
934 Status = Ip4SetAddress (IpIf, Ip, Netmask);
935
936 if (EFI_ERROR (Status)) {
937 Status = EFI_DEVICE_ERROR;
938 Ip4FreeInterface (IpIf, IpInstance);
939 goto ON_ERROR;
940 }
941
942 InsertTailList (&IpSb->Interfaces, &IpIf->Link);
943 }
944
945 //
946 // Add a route to this connected network in the route table
947 //
948 Ip4AddRoute (IpInstance->RouteTable, Ip, Netmask, IP4_ALLZERO_ADDRESS);
949
950 } else {
951 //
952 // Use the default address. If the default configuration hasn't
953 // been started, start it.
954 //
955 if (IpSb->State == IP4_SERVICE_UNSTARTED) {
956 Status = Ip4StartAutoConfig (IpSb);
957
958 if (EFI_ERROR (Status)) {
959 goto ON_ERROR;
960 }
961 }
962
963 IpIf = IpSb->DefaultInterface;
964 NET_GET_REF (IpSb->DefaultInterface);
965
966 //
967 // If default address is used, so is the default route table.
968 // Any route set by the instance has the precedence over the
969 // routes in the default route table. Link the default table
970 // after the instance's table. Routing will search the local
971 // table first.
972 //
973 NET_GET_REF (IpSb->DefaultRouteTable);
974 IpInstance->RouteTable->Next = IpSb->DefaultRouteTable;
975 }
976
977 IpInstance->Interface = IpIf;
978 InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);
979
980 CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));
981 IpInstance->State = IP4_STATE_CONFIGED;
982
983 //
984 // Although EFI_NO_MAPPING is an error code, the IP child has been
985 // successfully configured and doesn't need reconfiguration when
986 // default address is acquired.
987 //
988 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
989 return EFI_NO_MAPPING;
990 }
991
992 return EFI_SUCCESS;
993
994 ON_ERROR:
995 Ip4FreeRouteTable (IpInstance->RouteTable);
996 IpInstance->RouteTable = NULL;
997 return Status;
998 }
999
1000
1001 /**
1002 Clean up the IP4 child, release all the resources used by it.
1003
1004 @param[in] IpInstance The IP4 child to clean up.
1005
1006 @retval EFI_SUCCESS The IP4 child is cleaned up
1007 @retval EFI_DEVICE_ERROR Some resources failed to be released
1008
1009 **/
1010 EFI_STATUS
1011 Ip4CleanProtocol (
1012 IN IP4_PROTOCOL *IpInstance
1013 )
1014 {
1015 if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) {
1016 return EFI_DEVICE_ERROR;
1017 }
1018
1019 if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) {
1020 return EFI_DEVICE_ERROR;
1021 }
1022
1023 //
1024 // Some packets haven't been recycled. It is because either the
1025 // user forgets to recycle the packets, or because the callback
1026 // hasn't been called. Just leave it alone.
1027 //
1028 if (!IsListEmpty (&IpInstance->Delivered)) {
1029 ;
1030 }
1031
1032 if (IpInstance->Interface != NULL) {
1033 RemoveEntryList (&IpInstance->AddrLink);
1034 Ip4FreeInterface (IpInstance->Interface, IpInstance);
1035 IpInstance->Interface = NULL;
1036 }
1037
1038 if (IpInstance->RouteTable != NULL) {
1039 if (IpInstance->RouteTable->Next != NULL) {
1040 Ip4FreeRouteTable (IpInstance->RouteTable->Next);
1041 }
1042
1043 Ip4FreeRouteTable (IpInstance->RouteTable);
1044 IpInstance->RouteTable = NULL;
1045 }
1046
1047 if (IpInstance->EfiRouteTable != NULL) {
1048 FreePool (IpInstance->EfiRouteTable);
1049 IpInstance->EfiRouteTable = NULL;
1050 IpInstance->EfiRouteCount = 0;
1051 }
1052
1053 if (IpInstance->Groups != NULL) {
1054 FreePool (IpInstance->Groups);
1055 IpInstance->Groups = NULL;
1056 IpInstance->GroupCount = 0;
1057 }
1058
1059 NetMapClean (&IpInstance->TxTokens);
1060
1061 NetMapClean (&IpInstance->RxTokens);
1062
1063 return EFI_SUCCESS;
1064 }
1065
1066
1067 /**
1068 Validate that Ip/Netmask pair is OK to be used as station
1069 address. Only continuous netmasks are supported. and check
1070 that StationAddress is a unicast address on the newtwork.
1071
1072 @param[in] Ip The IP address to validate
1073 @param[in] Netmask The netmaks of the IP
1074
1075 @retval TRUE The Ip/Netmask pair is valid
1076 @retval FALSE The Ip/Netmask pair is invalid
1077
1078 **/
1079 BOOLEAN
1080 Ip4StationAddressValid (
1081 IN IP4_ADDR Ip,
1082 IN IP4_ADDR Netmask
1083 )
1084 {
1085 IP4_ADDR NetBrdcastMask;
1086 INTN Len;
1087 INTN Type;
1088
1089 //
1090 // Only support the station address with 0.0.0.0/0 to enable DHCP client.
1091 //
1092 if (Netmask == IP4_ALLZERO_ADDRESS) {
1093 return (BOOLEAN) (Ip == IP4_ALLZERO_ADDRESS);
1094 }
1095
1096 //
1097 // Only support the continuous net masks
1098 //
1099 if ((Len = NetGetMaskLength (Netmask)) == IP4_MASK_NUM) {
1100 return FALSE;
1101 }
1102
1103 //
1104 // Station address can't be class D or class E address
1105 //
1106 if ((Type = NetGetIpClass (Ip)) > IP4_ADDR_CLASSC) {
1107 return FALSE;
1108 }
1109
1110 //
1111 // Station address can't be subnet broadcast/net broadcast address
1112 //
1113 if ((Ip == (Ip & Netmask)) || (Ip == (Ip | ~Netmask))) {
1114 return FALSE;
1115 }
1116
1117 NetBrdcastMask = gIp4AllMasks[MIN (Len, Type << 3)];
1118
1119 if (Ip == (Ip | ~NetBrdcastMask)) {
1120 return FALSE;
1121 }
1122
1123 return TRUE;
1124 }
1125
1126
1127 /**
1128 Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.
1129
1130 The Configure() function is used to set, change, or reset the operational
1131 parameters and filter settings for this EFI IPv4 Protocol instance. Until these
1132 parameters have been set, no network traffic can be sent or received by this
1133 instance. Once the parameters have been reset (by calling this function with
1134 IpConfigData set to NULL), no more traffic can be sent or received until these
1135 parameters have been set again. Each EFI IPv4 Protocol instance can be started
1136 and stopped independently of each other by enabling or disabling their receive
1137 filter settings with the Configure() function.
1138
1139 When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will
1140 be appended as an alias address into the addresses list in the EFI IPv4 Protocol
1141 driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL
1142 to retrieve the default IPv4 address if it is not available yet. Clients could
1143 frequently call GetModeData() to check the status to ensure that the default IPv4
1144 address is ready.
1145
1146 If operational parameters are reset or changed, any pending transmit and receive
1147 requests will be cancelled. Their completion token status will be set to EFI_ABORTED
1148 and their events will be signaled.
1149
1150 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
1151 @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.
1152
1153 @retval EFI_SUCCESS The driver instance was successfully opened.
1154 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
1155 RARP, etc.) is not finished yet.
1156 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1157 @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:
1158 A configuration protocol (DHCP, BOOTP, RARP, etc.) could
1159 not be located when clients choose to use the default IPv4
1160 address. This EFI IPv4 Protocol implementation does not
1161 support this requested filter or timeout setting.
1162 @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.
1163 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the
1164 IPv4 address or subnet mask can be changed. The interface must
1165 also be stopped when switching to/from raw packet mode.
1166 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4
1167 Protocol driver instance is not opened.
1168
1169 **/
1170 EFI_STATUS
1171 EFIAPI
1172 EfiIp4Configure (
1173 IN EFI_IP4_PROTOCOL *This,
1174 IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL
1175 )
1176 {
1177 IP4_PROTOCOL *IpInstance;
1178 EFI_IP4_CONFIG_DATA *Current;
1179 EFI_TPL OldTpl;
1180 EFI_STATUS Status;
1181 BOOLEAN AddrOk;
1182 IP4_ADDR IpAddress;
1183 IP4_ADDR SubnetMask;
1184
1185 //
1186 // First, validate the parameters
1187 //
1188 if (This == NULL) {
1189 return EFI_INVALID_PARAMETER;
1190 }
1191
1192 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
1193 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1194
1195 //
1196 // Validate the configuration first.
1197 //
1198 if (IpConfigData != NULL) {
1199 //
1200 // This implementation doesn't support RawData
1201 //
1202 if (IpConfigData->RawData) {
1203 Status = EFI_UNSUPPORTED;
1204 goto ON_EXIT;
1205 }
1206
1207
1208 CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));
1209 CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));
1210
1211 IpAddress = NTOHL (IpAddress);
1212 SubnetMask = NTOHL (SubnetMask);
1213
1214 //
1215 // Check whether the station address is a valid unicast address
1216 //
1217 if (!IpConfigData->UseDefaultAddress) {
1218 AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask);
1219
1220 if (!AddrOk) {
1221 Status = EFI_INVALID_PARAMETER;
1222 goto ON_EXIT;
1223 }
1224 }
1225
1226 //
1227 // User can only update packet filters when already configured.
1228 // If it wants to change the station address, it must configure(NULL)
1229 // the instance first.
1230 //
1231 if (IpInstance->State == IP4_STATE_CONFIGED) {
1232 Current = &IpInstance->ConfigData;
1233
1234 if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) {
1235 Status = EFI_ALREADY_STARTED;
1236 goto ON_EXIT;
1237 }
1238
1239 if (!Current->UseDefaultAddress &&
1240 (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) ||
1241 !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask))) {
1242 Status = EFI_ALREADY_STARTED;
1243 goto ON_EXIT;
1244 }
1245
1246 if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
1247 return EFI_NO_MAPPING;
1248 }
1249 }
1250 }
1251
1252 //
1253 // Configure the instance or clean it up.
1254 //
1255 if (IpConfigData != NULL) {
1256 Status = Ip4ConfigProtocol (IpInstance, IpConfigData);
1257 } else {
1258 Status = Ip4CleanProtocol (IpInstance);
1259
1260 //
1261 // Don't change the state if it is DESTORY, consider the following
1262 // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,
1263 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,
1264 // the unload fails miserably.
1265 //
1266 if (IpInstance->State == IP4_STATE_CONFIGED) {
1267 IpInstance->State = IP4_STATE_UNCONFIGED;
1268 }
1269 }
1270
1271 //
1272 // Update the MNP's configure data. Ip4ServiceConfigMnp will check
1273 // whether it is necessary to reconfigure the MNP.
1274 //
1275 Ip4ServiceConfigMnp (IpInstance->Service, FALSE);
1276
1277 //
1278 // Update the variable data.
1279 //
1280 Ip4SetVariableData (IpInstance->Service);
1281
1282 ON_EXIT:
1283 gBS->RestoreTPL (OldTpl);
1284 return Status;
1285
1286 }
1287
1288
1289 /**
1290 Change the IP4 child's multicast setting. The caller
1291 should make sure that the parameters is valid.
1292
1293 @param[in] IpInstance The IP4 child to change the setting.
1294 @param[in] JoinFlag TRUE to join the group, otherwise leave it
1295 @param[in] GroupAddress The target group address
1296
1297 @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it
1298 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
1299 @retval EFI_DEVICE_ERROR Failed to set the group configuraton
1300 @retval EFI_SUCCESS Successfully updated the group setting.
1301 @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.
1302
1303 **/
1304 EFI_STATUS
1305 Ip4Groups (
1306 IN IP4_PROTOCOL *IpInstance,
1307 IN BOOLEAN JoinFlag,
1308 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
1309 )
1310 {
1311 IP4_ADDR *Members;
1312 IP4_ADDR Group;
1313 UINT32 Index;
1314
1315 //
1316 // Add it to the instance's Groups, and join the group by IGMP.
1317 // IpInstance->Groups is in network byte order. IGMP operates in
1318 // host byte order
1319 //
1320 if (JoinFlag) {
1321 //
1322 // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.
1323 //
1324 ASSERT (GroupAddress != NULL);
1325 CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));
1326
1327 for (Index = 0; Index < IpInstance->GroupCount; Index++) {
1328 if (IpInstance->Groups[Index] == Group) {
1329 return EFI_ALREADY_STARTED;
1330 }
1331 }
1332
1333 Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group);
1334
1335 if (Members == NULL) {
1336 return EFI_OUT_OF_RESOURCES;
1337 }
1338
1339 if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {
1340 FreePool (Members);
1341 return EFI_DEVICE_ERROR;
1342 }
1343
1344 if (IpInstance->Groups != NULL) {
1345 FreePool (IpInstance->Groups);
1346 }
1347
1348 IpInstance->Groups = Members;
1349 IpInstance->GroupCount++;
1350
1351 return EFI_SUCCESS;
1352 }
1353
1354 //
1355 // Leave the group. Leave all the groups if GroupAddress is NULL.
1356 // Must iterate from the end to the beginning because the GroupCount
1357 // is decreamented each time an address is removed..
1358 //
1359 for (Index = IpInstance->GroupCount; Index > 0 ; Index--) {
1360 Group = IpInstance->Groups[Index - 1];
1361
1362 if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {
1363 if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {
1364 return EFI_DEVICE_ERROR;
1365 }
1366
1367 Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group);
1368 IpInstance->GroupCount--;
1369
1370 if (IpInstance->GroupCount == 0) {
1371 ASSERT (Index == 1);
1372
1373 FreePool (IpInstance->Groups);
1374 IpInstance->Groups = NULL;
1375 }
1376
1377 if (GroupAddress != NULL) {
1378 return EFI_SUCCESS;
1379 }
1380 }
1381 }
1382
1383 return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);
1384 }
1385
1386
1387 /**
1388 Joins and leaves multicast groups.
1389
1390 The Groups() function is used to join and leave multicast group sessions. Joining
1391 a group will enable reception of matching multicast packets. Leaving a group will
1392 disable the multicast packet reception.
1393
1394 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
1395
1396 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
1397 @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.
1398 @param[in] GroupAddress Pointer to the IPv4 multicast address.
1399
1400 @retval EFI_SUCCESS The operation completed successfully.
1401 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
1402 - This is NULL.
1403 - JoinFlag is TRUE and GroupAddress is NULL.
1404 - GroupAddress is not NULL and *GroupAddress is
1405 not a multicast IPv4 address.
1406 @retval EFI_NOT_STARTED This instance has not been started.
1407 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
1408 RARP, etc.) is not finished yet.
1409 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
1410 @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.
1411 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
1412 JoinFlag is TRUE).
1413 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
1414 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1415
1416 **/
1417 EFI_STATUS
1418 EFIAPI
1419 EfiIp4Groups (
1420 IN EFI_IP4_PROTOCOL *This,
1421 IN BOOLEAN JoinFlag,
1422 IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
1423 )
1424 {
1425 IP4_PROTOCOL *IpInstance;
1426 EFI_STATUS Status;
1427 EFI_TPL OldTpl;
1428 IP4_ADDR McastIp;
1429
1430 if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) {
1431 return EFI_INVALID_PARAMETER;
1432 }
1433
1434 if (GroupAddress != NULL) {
1435 CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR));
1436
1437 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {
1438 return EFI_INVALID_PARAMETER;
1439 }
1440 }
1441
1442 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
1443 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1444
1445 if (IpInstance->State != IP4_STATE_CONFIGED) {
1446 Status = EFI_NOT_STARTED;
1447 goto ON_EXIT;
1448 }
1449
1450 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
1451 Status = EFI_NO_MAPPING;
1452 goto ON_EXIT;
1453 }
1454
1455 Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress);
1456
1457 ON_EXIT:
1458 gBS->RestoreTPL (OldTpl);
1459 return Status;
1460 }
1461
1462
1463 /**
1464 Adds and deletes routing table entries.
1465
1466 The Routes() function adds a route to or deletes a route from the routing table.
1467
1468 Routes are determined by comparing the SubnetAddress with the destination IPv4
1469 address arithmetically AND-ed with the SubnetMask. The gateway address must be
1470 on the same subnet as the configured station address.
1471
1472 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
1473 The default route matches all destination IPv4 addresses that do not match any
1474 other routes.
1475
1476 A GatewayAddress that is zero is a nonroute. Packets are sent to the destination
1477 IP address if it can be found in the ARP cache or on the local subnet. One automatic
1478 nonroute entry will be inserted into the routing table for outgoing packets that
1479 are addressed to a local subnet (gateway address of 0.0.0.0).
1480
1481 Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI
1482 IPv4 Protocol instances that use the default IPv4 address will also have copies
1483 of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these
1484 copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its
1485 instances. As a result, client modification to the routing table will be lost.
1486
1487 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
1488 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
1489 FALSE to add this route to the routing table. SubnetAddress
1490 and SubnetMask are used as the key to each route entry.
1491 @param[in] SubnetAddress The address of the subnet that needs to be routed.
1492 @param[in] SubnetMask The subnet mask of SubnetAddress.
1493 @param[in] GatewayAddress The unicast gateway IPv4 address for this route.
1494
1495 @retval EFI_SUCCESS The operation completed successfully.
1496 @retval EFI_NOT_STARTED The driver instance has not been started.
1497 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
1498 RARP, etc.) is not finished yet.
1499 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1500 - This is NULL.
1501 - SubnetAddress is NULL.
1502 - SubnetMask is NULL.
1503 - GatewayAddress is NULL.
1504 - *SubnetAddress is not a valid subnet address.
1505 - *SubnetMask is not a valid subnet mask.
1506 - *GatewayAddress is not a valid unicast IPv4 address.
1507 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
1508 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
1509 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
1510 DeleteRoute is FALSE).
1511
1512 **/
1513 EFI_STATUS
1514 EFIAPI
1515 EfiIp4Routes (
1516 IN EFI_IP4_PROTOCOL *This,
1517 IN BOOLEAN DeleteRoute,
1518 IN EFI_IPv4_ADDRESS *SubnetAddress,
1519 IN EFI_IPv4_ADDRESS *SubnetMask,
1520 IN EFI_IPv4_ADDRESS *GatewayAddress
1521 )
1522 {
1523 IP4_PROTOCOL *IpInstance;
1524 IP4_INTERFACE *IpIf;
1525 IP4_ADDR Dest;
1526 IP4_ADDR Netmask;
1527 IP4_ADDR Nexthop;
1528 EFI_STATUS Status;
1529 EFI_TPL OldTpl;
1530
1531 //
1532 // First, validate the parameters
1533 //
1534 if ((This == NULL) || (SubnetAddress == NULL) ||
1535 (SubnetMask == NULL) || (GatewayAddress == NULL)) {
1536 return EFI_INVALID_PARAMETER;
1537 }
1538
1539 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
1540 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1541
1542 if (IpInstance->State != IP4_STATE_CONFIGED) {
1543 Status = EFI_NOT_STARTED;
1544 goto ON_EXIT;
1545 }
1546
1547 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
1548 Status = EFI_NO_MAPPING;
1549 goto ON_EXIT;
1550 }
1551
1552 CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR));
1553 CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR));
1554 CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR));
1555
1556 Dest = NTOHL (Dest);
1557 Netmask = NTOHL (Netmask);
1558 Nexthop = NTOHL (Nexthop);
1559
1560 IpIf = IpInstance->Interface;
1561
1562 if (!IP4_IS_VALID_NETMASK (Netmask)) {
1563 Status = EFI_INVALID_PARAMETER;
1564 goto ON_EXIT;
1565 }
1566
1567 //
1568 // the gateway address must be a unicast on the connected network if not zero.
1569 //
1570 if ((Nexthop != IP4_ALLZERO_ADDRESS) &&
1571 (!IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask) ||
1572 IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf)))) {
1573
1574 Status = EFI_INVALID_PARAMETER;
1575 goto ON_EXIT;
1576 }
1577
1578 if (DeleteRoute) {
1579 Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);
1580 } else {
1581 Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);
1582 }
1583
1584 ON_EXIT:
1585 gBS->RestoreTPL (OldTpl);
1586 return Status;
1587 }
1588
1589
1590 /**
1591 Check whether the user's token or event has already
1592 been enqueued on IP4's list.
1593
1594 @param[in] Map The container of either user's transmit or receive
1595 token.
1596 @param[in] Item Current item to check against
1597 @param[in] Context The Token to check againist.
1598
1599 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
1600 @retval EFI_SUCCESS The current item isn't the same token/event as the
1601 context.
1602
1603 **/
1604 EFI_STATUS
1605 Ip4TokenExist (
1606 IN NET_MAP *Map,
1607 IN NET_MAP_ITEM *Item,
1608 IN VOID *Context
1609 )
1610 {
1611 EFI_IP4_COMPLETION_TOKEN *Token;
1612 EFI_IP4_COMPLETION_TOKEN *TokenInItem;
1613
1614 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;
1615 TokenInItem = (EFI_IP4_COMPLETION_TOKEN *) Item->Key;
1616
1617 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
1618 return EFI_ACCESS_DENIED;
1619 }
1620
1621 return EFI_SUCCESS;
1622 }
1623
1624
1625 /**
1626 Validate the user's token against current station address.
1627
1628 @param[in] Token User's token to validate
1629 @param[in] IpIf The IP4 child's interface.
1630
1631 @retval EFI_INVALID_PARAMETER Some parameters are invalid
1632 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.
1633 @retval EFI_SUCCESS The token is OK
1634
1635 **/
1636 EFI_STATUS
1637 Ip4TxTokenValid (
1638 IN EFI_IP4_COMPLETION_TOKEN *Token,
1639 IN IP4_INTERFACE *IpIf
1640 )
1641 {
1642 EFI_IP4_TRANSMIT_DATA *TxData;
1643 EFI_IP4_OVERRIDE_DATA *Override;
1644 IP4_ADDR Src;
1645 IP4_ADDR Gateway;
1646 UINT32 Offset;
1647 UINT32 Index;
1648 UINT32 HeadLen;
1649
1650 if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) {
1651 return EFI_INVALID_PARAMETER;
1652 }
1653
1654 TxData = Token->Packet.TxData;
1655
1656 //
1657 // Check the IP options: no more than 40 bytes and format is OK
1658 //
1659 if (TxData->OptionsLength != 0) {
1660 if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {
1661 return EFI_INVALID_PARAMETER;
1662 }
1663
1664 if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {
1665 return EFI_INVALID_PARAMETER;
1666 }
1667 }
1668
1669 //
1670 // Check the fragment table: no empty fragment, and length isn't bogus
1671 //
1672 if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {
1673 return EFI_INVALID_PARAMETER;
1674 }
1675
1676 Offset = TxData->TotalDataLength;
1677
1678 for (Index = 0; Index < TxData->FragmentCount; Index++) {
1679 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||
1680 (TxData->FragmentTable[Index].FragmentLength == 0)) {
1681
1682 return EFI_INVALID_PARAMETER;
1683 }
1684
1685 Offset -= TxData->FragmentTable[Index].FragmentLength;
1686 }
1687
1688 if (Offset != 0) {
1689 return EFI_INVALID_PARAMETER;
1690 }
1691
1692 //
1693 // Check the source and gateway: they must be a valid unicast.
1694 // Gateway must also be on the connected network.
1695 //
1696 if (TxData->OverrideData != NULL) {
1697 Override = TxData->OverrideData;
1698
1699 CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));
1700 CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR));
1701
1702 Src = NTOHL (Src);
1703 Gateway = NTOHL (Gateway);
1704
1705 if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) ||
1706 (Src == IP4_ALLONE_ADDRESS) ||
1707 IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {
1708
1709 return EFI_INVALID_PARAMETER;
1710 }
1711
1712 //
1713 // If gateway isn't zero, it must be a unicast address, and
1714 // on the connected network.
1715 //
1716 if ((Gateway != IP4_ALLZERO_ADDRESS) &&
1717 ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) ||
1718 !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) ||
1719 IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf)))) {
1720
1721 return EFI_INVALID_PARAMETER;
1722 }
1723 }
1724
1725 //
1726 // Check the packet length: Head length and packet length all has a limit
1727 //
1728 HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);
1729
1730 if ((HeadLen > IP4_MAX_HEADLEN) ||
1731 (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE)) {
1732
1733 return EFI_BAD_BUFFER_SIZE;
1734 }
1735
1736 return EFI_SUCCESS;
1737 }
1738
1739
1740 /**
1741 The callback function for the net buffer which wraps the user's
1742 transmit token. Although it seems this function is pretty simple,
1743 there are some subtle things.
1744 When user requests the IP to transmit a packet by passing it a
1745 token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data
1746 is wrapped in an net buffer. the net buffer's Free function is
1747 set to Ip4FreeTxToken. The Token and token wrap are added to the
1748 IP child's TxToken map. Then the buffer is passed to Ip4Output for
1749 transmission. If something error happened before that, the buffer
1750 is freed, which in turn will free the token wrap. The wrap may
1751 have been added to the TxToken map or not, and the user's event
1752 shouldn't be fired because we are still in the EfiIp4Transmit. If
1753 the buffer has been sent by Ip4Output, it should be removed from
1754 the TxToken map and user's event signaled. The token wrap and buffer
1755 are bound together. Check the comments in Ip4Output for information
1756 about IP fragmentation.
1757
1758 @param[in] Context The token's wrap
1759
1760 **/
1761 VOID
1762 Ip4FreeTxToken (
1763 IN VOID *Context
1764 )
1765 {
1766 IP4_TXTOKEN_WRAP *Wrap;
1767 NET_MAP_ITEM *Item;
1768
1769 Wrap = (IP4_TXTOKEN_WRAP *) Context;
1770
1771 //
1772 // Signal IpSecRecycleEvent to inform IPsec free the memory
1773 //
1774 if (Wrap->IpSecRecycleSignal != NULL) {
1775 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
1776 }
1777
1778 //
1779 // Find the token in the instance's map. EfiIp4Transmit put the
1780 // token to the map. If that failed, NetMapFindKey will return NULL.
1781 //
1782 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);
1783
1784 if (Item != NULL) {
1785 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);
1786 }
1787
1788 if (Wrap->Sent) {
1789 gBS->SignalEvent (Wrap->Token->Event);
1790
1791 //
1792 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
1793 //
1794 DispatchDpc ();
1795 }
1796
1797 FreePool (Wrap);
1798 }
1799
1800
1801 /**
1802 The callback function to Ip4Output to update the transmit status.
1803
1804 @param Ip4Instance The Ip4Instance that request the transmit.
1805 @param Packet The user's transmit request
1806 @param IoStatus The result of the transmission
1807 @param Flag Not used during transmission
1808 @param Context The token's wrap.
1809
1810 **/
1811 VOID
1812 Ip4OnPacketSent (
1813 IP4_PROTOCOL *Ip4Instance,
1814 NET_BUF *Packet,
1815 EFI_STATUS IoStatus,
1816 UINT32 Flag,
1817 VOID *Context
1818 )
1819 {
1820 IP4_TXTOKEN_WRAP *Wrap;
1821
1822 //
1823 // This is the transmission request from upper layer,
1824 // not the IP4 driver itself.
1825 //
1826 ASSERT (Ip4Instance != NULL);
1827
1828 //
1829 // The first fragment of the packet has been sent. Update
1830 // the token's status. That is, if fragmented, the transmit's
1831 // status is the first fragment's status. The Wrap will be
1832 // release when all the fragments are release. Check the comments
1833 // in Ip4FreeTxToken and Ip4Output for information.
1834 //
1835 Wrap = (IP4_TXTOKEN_WRAP *) Context;
1836 Wrap->Token->Status = IoStatus;
1837
1838 NetbufFree (Wrap->Packet);
1839 }
1840
1841
1842 /**
1843 Places outgoing data packets into the transmit queue.
1844
1845 The Transmit() function places a sending request in the transmit queue of this
1846 EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some
1847 errors occur, the event in the token will be signaled and the status is updated.
1848
1849 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
1850 @param[in] Token Pointer to the transmit token.
1851
1852 @retval EFI_SUCCESS The data has been queued for transmission.
1853 @retval EFI_NOT_STARTED This instance has not been started.
1854 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
1855 RARP, etc.) is not finished yet.
1856 @retval EFI_INVALID_PARAMETER One or more pameters are invalid.
1857 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event
1858 was already in the transmit queue.
1859 @retval EFI_NOT_READY The completion token could not be queued because the transmit
1860 queue is full.
1861 @retval EFI_NOT_FOUND Not route is found to destination address.
1862 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
1863 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
1864 short to transmit.
1865 @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is
1866 greater than MTU (or greater than the maximum packet size if
1867 Token.Packet.TxData.OverrideData.
1868 DoNotFragment is TRUE.)
1869
1870 **/
1871 EFI_STATUS
1872 EFIAPI
1873 EfiIp4Transmit (
1874 IN EFI_IP4_PROTOCOL *This,
1875 IN EFI_IP4_COMPLETION_TOKEN *Token
1876 )
1877 {
1878 IP4_SERVICE *IpSb;
1879 IP4_PROTOCOL *IpInstance;
1880 IP4_INTERFACE *IpIf;
1881 IP4_TXTOKEN_WRAP *Wrap;
1882 EFI_IP4_TRANSMIT_DATA *TxData;
1883 EFI_IP4_CONFIG_DATA *Config;
1884 EFI_IP4_OVERRIDE_DATA *Override;
1885 IP4_HEAD Head;
1886 IP4_ADDR GateWay;
1887 EFI_STATUS Status;
1888 EFI_TPL OldTpl;
1889 BOOLEAN DontFragment;
1890 UINT32 HeadLen;
1891
1892 if (This == NULL) {
1893 return EFI_INVALID_PARAMETER;
1894 }
1895
1896 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
1897
1898 if (IpInstance->State != IP4_STATE_CONFIGED) {
1899 return EFI_NOT_STARTED;
1900 }
1901
1902 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1903
1904 IpSb = IpInstance->Service;
1905 IpIf = IpInstance->Interface;
1906 Config = &IpInstance->ConfigData;
1907
1908 if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
1909 Status = EFI_NO_MAPPING;
1910 goto ON_EXIT;
1911 }
1912
1913 //
1914 // make sure that token is properly formated
1915 //
1916 Status = Ip4TxTokenValid (Token, IpIf);
1917
1918 if (EFI_ERROR (Status)) {
1919 goto ON_EXIT;
1920 }
1921
1922 //
1923 // Check whether the token or signal already existed.
1924 //
1925 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) {
1926 Status = EFI_ACCESS_DENIED;
1927 goto ON_EXIT;
1928 }
1929
1930 //
1931 // Build the IP header, need to fill in the Tos, TotalLen, Id,
1932 // fragment, Ttl, protocol, Src, and Dst.
1933 //
1934 TxData = Token->Packet.TxData;
1935
1936 CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));
1937 Head.Dst = NTOHL (Head.Dst);
1938
1939 if (TxData->OverrideData != NULL) {
1940 Override = TxData->OverrideData;
1941 Head.Protocol = Override->Protocol;
1942 Head.Tos = Override->TypeOfService;
1943 Head.Ttl = Override->TimeToLive;
1944 DontFragment = Override->DoNotFragment;
1945
1946 CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));
1947 CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));
1948
1949 Head.Src = NTOHL (Head.Src);
1950 GateWay = NTOHL (GateWay);
1951 } else {
1952 Head.Src = IpIf->Ip;
1953 GateWay = IP4_ALLZERO_ADDRESS;
1954 Head.Protocol = Config->DefaultProtocol;
1955 Head.Tos = Config->TypeOfService;
1956 Head.Ttl = Config->TimeToLive;
1957 DontFragment = Config->DoNotFragment;
1958 }
1959
1960 Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);
1961 HeadLen = (TxData->OptionsLength + 3) & (~0x03);
1962
1963 //
1964 // If don't fragment and fragment needed, return error
1965 //
1966 if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {
1967 Status = EFI_BAD_BUFFER_SIZE;
1968 goto ON_EXIT;
1969 }
1970
1971 //
1972 // OK, it survives all the validation check. Wrap the token in
1973 // a IP4_TXTOKEN_WRAP and the data in a netbuf
1974 //
1975 Status = EFI_OUT_OF_RESOURCES;
1976 Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));
1977 if (Wrap == NULL) {
1978 goto ON_EXIT;
1979 }
1980
1981 Wrap->IpInstance = IpInstance;
1982 Wrap->Token = Token;
1983 Wrap->Sent = FALSE;
1984 Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout);
1985 Wrap->Packet = NetbufFromExt (
1986 (NET_FRAGMENT *) TxData->FragmentTable,
1987 TxData->FragmentCount,
1988 IP4_MAX_HEADLEN,
1989 0,
1990 Ip4FreeTxToken,
1991 Wrap
1992 );
1993
1994 if (Wrap->Packet == NULL) {
1995 FreePool (Wrap);
1996 goto ON_EXIT;
1997 }
1998
1999 Token->Status = EFI_NOT_READY;
2000
2001 if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) {
2002 //
2003 // NetbufFree will call Ip4FreeTxToken, which in turn will
2004 // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been
2005 // enqueued.
2006 //
2007 NetbufFree (Wrap->Packet);
2008 goto ON_EXIT;
2009 }
2010
2011 //
2012 // Mark the packet sent before output it. Mark it not sent again if the
2013 // returned status is not EFI_SUCCESS;
2014 //
2015 Wrap->Sent = TRUE;
2016
2017 Status = Ip4Output (
2018 IpSb,
2019 IpInstance,
2020 Wrap->Packet,
2021 &Head,
2022 TxData->OptionsBuffer,
2023 TxData->OptionsLength,
2024 GateWay,
2025 Ip4OnPacketSent,
2026 Wrap
2027 );
2028
2029 if (EFI_ERROR (Status)) {
2030 Wrap->Sent = FALSE;
2031 NetbufFree (Wrap->Packet);
2032 }
2033
2034 ON_EXIT:
2035 gBS->RestoreTPL (OldTpl);
2036 return Status;
2037 }
2038
2039
2040 /**
2041 Places a receiving request into the receiving queue.
2042
2043 The Receive() function places a completion token into the receive packet queue.
2044 This function is always asynchronous.
2045
2046 The Token.Event field in the completion token must be filled in by the caller
2047 and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol
2048 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
2049 is signaled.
2050
2051 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
2052 @param[in] Token Pointer to a token that is associated with the receive data descriptor.
2053
2054 @retval EFI_SUCCESS The receive completion token was cached.
2055 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
2056 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)
2057 is not finished yet.
2058 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
2059 - This is NULL.
2060 - Token is NULL.
2061 - Token.Event is NULL.
2062 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
2063 resources (usually memory).
2064 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
2065 The EFI IPv4 Protocol instance has been reset to startup defaults.
2066 EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
2067 in the receive queue.
2068 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
2069 @retval EFI_ICMP_ERROR An ICMP error packet was received.
2070
2071 **/
2072 EFI_STATUS
2073 EFIAPI
2074 EfiIp4Receive (
2075 IN EFI_IP4_PROTOCOL *This,
2076 IN EFI_IP4_COMPLETION_TOKEN *Token
2077 )
2078 {
2079 IP4_PROTOCOL *IpInstance;
2080 EFI_STATUS Status;
2081 EFI_TPL OldTpl;
2082
2083 //
2084 // First validate the parameters
2085 //
2086 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
2087 return EFI_INVALID_PARAMETER;
2088 }
2089
2090 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
2091
2092 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
2093
2094 if (IpInstance->State != IP4_STATE_CONFIGED) {
2095 Status = EFI_NOT_STARTED;
2096 goto ON_EXIT;
2097 }
2098
2099 //
2100 // Check whether the toke is already on the receive queue.
2101 //
2102 Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token);
2103
2104 if (EFI_ERROR (Status)) {
2105 Status = EFI_ACCESS_DENIED;
2106 goto ON_EXIT;
2107 }
2108
2109 //
2110 // Queue the token then check whether there is pending received packet.
2111 //
2112 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);
2113
2114 if (EFI_ERROR (Status)) {
2115 goto ON_EXIT;
2116 }
2117
2118 Status = Ip4InstanceDeliverPacket (IpInstance);
2119
2120 //
2121 // Dispatch the DPC queued by the NotifyFunction of this instane's receive
2122 // event.
2123 //
2124 DispatchDpc ();
2125
2126 ON_EXIT:
2127 gBS->RestoreTPL (OldTpl);
2128 return Status;
2129 }
2130
2131
2132 /**
2133 Cancel the transmitted but not recycled packet. If a matching
2134 token is found, it will call Ip4CancelPacket to cancel the
2135 packet. Ip4CancelPacket will cancel all the fragments of the
2136 packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP
2137 will be deleted from the Map, and user's event signalled.
2138 Because Ip4CancelPacket and other functions are all called in
2139 line, so, after Ip4CancelPacket returns, the Item has been freed.
2140
2141 @param[in] Map The IP4 child's transmit queue
2142 @param[in] Item The current transmitted packet to test.
2143 @param[in] Context The user's token to cancel.
2144
2145 @retval EFI_SUCCESS Continue to check the next Item.
2146 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
2147
2148 **/
2149 EFI_STATUS
2150 Ip4CancelTxTokens (
2151 IN NET_MAP *Map,
2152 IN NET_MAP_ITEM *Item,
2153 IN VOID *Context
2154 )
2155 {
2156 EFI_IP4_COMPLETION_TOKEN *Token;
2157 IP4_TXTOKEN_WRAP *Wrap;
2158
2159 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;
2160
2161 //
2162 // Return EFI_SUCCESS to check the next item in the map if
2163 // this one doesn't match.
2164 //
2165 if ((Token != NULL) && (Token != Item->Key)) {
2166 return EFI_SUCCESS;
2167 }
2168
2169 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;
2170 ASSERT (Wrap != NULL);
2171
2172 //
2173 // Don't access the Item, Wrap and Token's members after this point.
2174 // Item and wrap has been freed. And we no longer own the Token.
2175 //
2176 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
2177
2178 //
2179 // If only one item is to be cancel, return EFI_ABORTED to stop
2180 // iterating the map any more.
2181 //
2182 if (Token != NULL) {
2183 return EFI_ABORTED;
2184 }
2185
2186 return EFI_SUCCESS;
2187 }
2188
2189
2190 /**
2191 Cancel the receive request. This is quiet simple, because
2192 it is only enqueued in our local receive map.
2193
2194 @param[in] Map The IP4 child's receive queue
2195 @param[in] Item Current receive request to cancel.
2196 @param[in] Context The user's token to cancel
2197
2198 @retval EFI_SUCCESS Continue to check the next receive request on the
2199 queue.
2200 @retval EFI_ABORTED The user's token (token != NULL) has been
2201 cancelled.
2202
2203 **/
2204 EFI_STATUS
2205 Ip4CancelRxTokens (
2206 IN NET_MAP *Map,
2207 IN NET_MAP_ITEM *Item,
2208 IN VOID *Context
2209 )
2210 {
2211 EFI_IP4_COMPLETION_TOKEN *Token;
2212 EFI_IP4_COMPLETION_TOKEN *This;
2213
2214 Token = (EFI_IP4_COMPLETION_TOKEN *) Context;
2215 This = Item->Key;
2216
2217 if ((Token != NULL) && (Token != This)) {
2218 return EFI_SUCCESS;
2219 }
2220
2221 NetMapRemoveItem (Map, Item, NULL);
2222
2223 This->Status = EFI_ABORTED;
2224 This->Packet.RxData = NULL;
2225 gBS->SignalEvent (This->Event);
2226
2227 if (Token != NULL) {
2228 return EFI_ABORTED;
2229 }
2230
2231 return EFI_SUCCESS;
2232 }
2233
2234
2235 /**
2236 Cancel the user's receive/transmit request.
2237
2238 @param[in] IpInstance The IP4 child
2239 @param[in] Token The token to cancel. If NULL, all token will be
2240 cancelled.
2241
2242 @retval EFI_SUCCESS The token is cancelled
2243 @retval EFI_NOT_FOUND The token isn't found on either the
2244 transmit/receive queue
2245 @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.
2246
2247 **/
2248 EFI_STATUS
2249 Ip4Cancel (
2250 IN IP4_PROTOCOL *IpInstance,
2251 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
2252 )
2253 {
2254 EFI_STATUS Status;
2255
2256 //
2257 // First check the transmitted packet. Ip4CancelTxTokens returns
2258 // EFI_ABORTED to mean that the token has been cancelled when
2259 // token != NULL. So, return EFI_SUCCESS for this condition.
2260 //
2261 Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token);
2262
2263 if (EFI_ERROR (Status)) {
2264 if ((Token != NULL) && (Status == EFI_ABORTED)) {
2265 return EFI_SUCCESS;
2266 }
2267
2268 return Status;
2269 }
2270
2271 //
2272 // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT
2273 // for Token!=NULL and it is cancelled.
2274 //
2275 Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token);
2276 //
2277 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
2278 // events.
2279 //
2280 DispatchDpc ();
2281 if (EFI_ERROR (Status)) {
2282 if ((Token != NULL) && (Status == EFI_ABORTED)) {
2283 return EFI_SUCCESS;
2284 }
2285
2286 return Status;
2287 }
2288
2289 //
2290 // OK, if the Token is found when Token != NULL, the NetMapIterate
2291 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.
2292 //
2293 if (Token != NULL) {
2294 return EFI_NOT_FOUND;
2295 }
2296
2297 //
2298 // If Token == NULL, cancel all the tokens. return error if no
2299 // all of them are cancelled.
2300 //
2301 if (!NetMapIsEmpty (&IpInstance->TxTokens) ||
2302 !NetMapIsEmpty (&IpInstance->RxTokens)) {
2303
2304 return EFI_DEVICE_ERROR;
2305 }
2306
2307 return EFI_SUCCESS;
2308 }
2309
2310
2311 /**
2312 Abort an asynchronous transmit or receive request.
2313
2314 The Cancel() function is used to abort a pending transmit or receive request.
2315 If the token is in the transmit or receive request queues, after calling this
2316 function, Token->Status will be set to EFI_ABORTED and then Token->Event will
2317 be signaled. If the token is not in one of the queues, which usually means the
2318 asynchronous operation has completed, this function will not signal the token
2319 and EFI_NOT_FOUND is returned.
2320
2321 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
2322 @param[in] Token Pointer to a token that has been issued by
2323 EFI_IP4_PROTOCOL.Transmit() or
2324 EFI_IP4_PROTOCOL.Receive(). If NULL, all pending
2325 tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is
2326 defined in EFI_IP4_PROTOCOL.Transmit().
2327
2328 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
2329 Token.->Event was signaled. When Token is NULL, all
2330 pending requests were aborted and their events were signaled.
2331 @retval EFI_INVALID_PARAMETER This is NULL.
2332 @retval EFI_NOT_STARTED This instance has not been started.
2333 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
2334 RARP, etc.) is not finished yet.
2335 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
2336 not found in the transmit or receive queue. It has either completed
2337 or was not issued by Transmit() and Receive().
2338
2339 **/
2340 EFI_STATUS
2341 EFIAPI
2342 EfiIp4Cancel (
2343 IN EFI_IP4_PROTOCOL *This,
2344 IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
2345 )
2346 {
2347 IP4_PROTOCOL *IpInstance;
2348 EFI_STATUS Status;
2349 EFI_TPL OldTpl;
2350
2351 if (This == NULL) {
2352 return EFI_INVALID_PARAMETER;
2353 }
2354
2355 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
2356
2357 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
2358
2359 if (IpInstance->State != IP4_STATE_CONFIGED) {
2360 Status = EFI_NOT_STARTED;
2361 goto ON_EXIT;
2362 }
2363
2364 if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
2365 Status = EFI_NO_MAPPING;
2366 goto ON_EXIT;
2367 }
2368
2369 Status = Ip4Cancel (IpInstance, Token);
2370
2371 ON_EXIT:
2372 gBS->RestoreTPL (OldTpl);
2373 return Status;
2374 }
2375
2376
2377 /**
2378 Polls for incoming data packets and processes outgoing data packets.
2379
2380 The Poll() function polls for incoming data packets and processes outgoing data
2381 packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()
2382 function to increase the rate that data packets are moved between the communications
2383 device and the transmit and receive queues.
2384
2385 In some systems the periodic timer event may not poll the underlying communications
2386 device fast enough to transmit and/or receive all data packets without missing
2387 incoming packets or dropping outgoing packets. Drivers and applications that are
2388 experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function
2389 more often.
2390
2391 @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
2392
2393 @retval EFI_SUCCESS Incoming or outgoing data was processed.
2394 @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
2395 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
2396 RARP, etc.) is not finished yet.
2397 @retval EFI_INVALID_PARAMETER This is NULL.
2398 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
2399 @retval EFI_NOT_READY No incoming or outgoing data is processed.
2400 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
2401 Consider increasing the polling rate.
2402
2403 **/
2404 EFI_STATUS
2405 EFIAPI
2406 EfiIp4Poll (
2407 IN EFI_IP4_PROTOCOL *This
2408 )
2409 {
2410 IP4_PROTOCOL *IpInstance;
2411 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
2412
2413 if (This == NULL) {
2414 return EFI_INVALID_PARAMETER;
2415 }
2416
2417 IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
2418
2419 if (IpInstance->State == IP4_STATE_UNCONFIGED) {
2420 return EFI_NOT_STARTED;
2421 }
2422
2423 Mnp = IpInstance->Service->Mnp;
2424
2425 //
2426 // Don't lock the Poll function to enable the deliver of
2427 // the packet polled up.
2428 //
2429 return Mnp->Poll (Mnp);
2430 }
2431
2432 /**
2433 Decrease the life of the transmitted packets. If it is
2434 decreased to zero, cancel the packet. This function is
2435 called by Ip4PacketTimerTicking which time out both the
2436 received-but-not-delivered and transmitted-but-not-recycle
2437 packets.
2438
2439 @param[in] Map The IP4 child's transmit map.
2440 @param[in] Item Current transmitted packet
2441 @param[in] Context Not used.
2442
2443 @retval EFI_SUCCESS Always returns EFI_SUCCESS
2444
2445 **/
2446 EFI_STATUS
2447 Ip4SentPacketTicking (
2448 IN NET_MAP *Map,
2449 IN NET_MAP_ITEM *Item,
2450 IN VOID *Context
2451 )
2452 {
2453 IP4_TXTOKEN_WRAP *Wrap;
2454
2455 Wrap = (IP4_TXTOKEN_WRAP *) Item->Value;
2456 ASSERT (Wrap != NULL);
2457
2458 if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {
2459 Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
2460 }
2461
2462 return EFI_SUCCESS;
2463 }
2464
2465
2466 /**
2467 The heart beat timer of IP4 service instance. It times out
2468 all of its IP4 children's received-but-not-delivered and
2469 transmitted-but-not-recycle packets, and provides time input
2470 for its IGMP protocol.
2471
2472 @param[in] Event The IP4 service instance's heart beat timer.
2473 @param[in] Context The IP4 service instance.
2474
2475 **/
2476 VOID
2477 EFIAPI
2478 Ip4TimerTicking (
2479 IN EFI_EVENT Event,
2480 IN VOID *Context
2481 )
2482 {
2483 IP4_SERVICE *IpSb;
2484
2485 IpSb = (IP4_SERVICE *) Context;
2486 NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);
2487
2488 Ip4PacketTimerTicking (IpSb);
2489 Ip4IgmpTicking (IpSb);
2490 }