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