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