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