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