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