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