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