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