]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Impl.c
621879b1ea579583af79b1340fb0a7b101a89e13
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Impl.c
1 /** @file
2 Implementation of EFI_IP6_PROTOCOL protocol interfaces.
3
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Ip6Impl.h"
17
18 EFI_IPSEC2_PROTOCOL *mIpSec = NULL;
19
20 EFI_IP6_PROTOCOL mEfiIp6ProtocolTemplete = {
21 EfiIp6GetModeData,
22 EfiIp6Configure,
23 EfiIp6Groups,
24 EfiIp6Routes,
25 EfiIp6Neighbors,
26 EfiIp6Transmit,
27 EfiIp6Receive,
28 EfiIp6Cancel,
29 EfiIp6Poll
30 };
31
32 /**
33 Gets the current operational settings for this instance of the EFI IPv6 Protocol driver.
34
35 The GetModeData() function returns the current operational mode data for this driver instance.
36 The data fields in EFI_IP6_MODE_DATA are read only. This function is used optionally to
37 retrieve the operational mode data of underlying networks or drivers.
38
39 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
40 @param[out] Ip6ModeData Pointer to the EFI IPv6 Protocol mode data structure.
41 @param[out] MnpConfigData Pointer to the managed network configuration data structure.
42 @param[out] SnpModeData Pointer to the simple network mode data structure.
43
44 @retval EFI_SUCCESS The operation completed successfully.
45 @retval EFI_INVALID_PARAMETER This is NULL.
46 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 EfiIp6GetModeData (
52 IN EFI_IP6_PROTOCOL *This,
53 OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL,
54 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
55 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
56 )
57 {
58 IP6_PROTOCOL *IpInstance;
59 IP6_SERVICE *IpSb;
60 IP6_INTERFACE *IpIf;
61 EFI_IP6_CONFIG_DATA *Config;
62 EFI_STATUS Status;
63 EFI_TPL OldTpl;
64
65 if (This == NULL) {
66 return EFI_INVALID_PARAMETER;
67 }
68
69 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
70 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
71 IpSb = IpInstance->Service;
72 IpIf = IpInstance->Interface;
73
74 if (IpSb->LinkLocalDadFail) {
75 return EFI_INVALID_PARAMETER;
76 }
77
78 if (Ip6ModeData != NULL) {
79 //
80 // IsStarted is "whether the EfiIp6Configure has been called".
81 // IsConfigured is "whether the station address has been configured"
82 //
83 Ip6ModeData->IsStarted = (BOOLEAN) (IpInstance->State == IP6_STATE_CONFIGED);
84 Ip6ModeData->MaxPacketSize = IpSb->MaxPacketSize;
85 CopyMem (&Ip6ModeData->ConfigData, &IpInstance->ConfigData, sizeof (EFI_IP6_CONFIG_DATA));
86 Ip6ModeData->IsConfigured = FALSE;
87
88 Ip6ModeData->AddressCount = 0;
89 Ip6ModeData->AddressList = NULL;
90
91 Ip6ModeData->GroupCount = IpInstance->GroupCount;
92 Ip6ModeData->GroupTable = NULL;
93
94 Ip6ModeData->RouteCount = 0;
95 Ip6ModeData->RouteTable = NULL;
96
97 Ip6ModeData->NeighborCount = 0;
98 Ip6ModeData->NeighborCache = NULL;
99
100 Ip6ModeData->PrefixCount = 0;
101 Ip6ModeData->PrefixTable = NULL;
102
103 Ip6ModeData->IcmpTypeCount = 23;
104 Ip6ModeData->IcmpTypeList = AllocateCopyPool (
105 Ip6ModeData->IcmpTypeCount * sizeof (EFI_IP6_ICMP_TYPE),
106 mIp6SupportedIcmp
107 );
108 if (Ip6ModeData->IcmpTypeList == NULL) {
109 Status = EFI_OUT_OF_RESOURCES;
110 goto Error;
111 }
112
113 //
114 // Return the currently configured IPv6 addresses and corresponding prefix lengths.
115 //
116 Status = Ip6BuildEfiAddressList (
117 IpSb,
118 &Ip6ModeData->AddressCount,
119 &Ip6ModeData->AddressList
120 );
121 if (EFI_ERROR (Status)) {
122 goto Error;
123 }
124
125 //
126 // Return the current station address for this IP child.
127 // If UseAnyStationAddress is set to TRUE, IP6 driver will
128 // select a source address from its address list. Otherwise use the
129 // StationAddress in config data.
130 //
131 if (Ip6ModeData->IsStarted) {
132 Config = &Ip6ModeData->ConfigData;
133
134 if (IpIf->Configured || NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) {
135 Ip6ModeData->IsConfigured = TRUE;
136 } else {
137 Ip6ModeData->IsConfigured = FALSE;
138 }
139
140 //
141 // Build a EFI route table for user from the internal route table.
142 //
143 Status = Ip6BuildEfiRouteTable (
144 IpSb->RouteTable,
145 &Ip6ModeData->RouteCount,
146 &Ip6ModeData->RouteTable
147 );
148
149 if (EFI_ERROR (Status)) {
150 goto Error;
151 }
152 }
153
154 if (Ip6ModeData->IsConfigured) {
155 //
156 // Return the joined multicast group addresses.
157 //
158 if (IpInstance->GroupCount != 0) {
159 Ip6ModeData->GroupTable = AllocateCopyPool (
160 IpInstance->GroupCount * sizeof (EFI_IPv6_ADDRESS),
161 IpInstance->GroupList
162 );
163 if (Ip6ModeData->GroupTable == NULL) {
164 Status = EFI_OUT_OF_RESOURCES;
165 goto Error;
166 }
167 }
168 //
169 // Return the neighbor cache entries
170 //
171 Status = Ip6BuildEfiNeighborCache (
172 IpInstance,
173 &Ip6ModeData->NeighborCount,
174 &Ip6ModeData->NeighborCache
175 );
176 if (EFI_ERROR (Status)) {
177 goto Error;
178 }
179
180 //
181 // Return the prefix table entries
182 //
183 Status = Ip6BuildPrefixTable (
184 IpInstance,
185 &Ip6ModeData->PrefixCount,
186 &Ip6ModeData->PrefixTable
187 );
188 if (EFI_ERROR (Status)) {
189 goto Error;
190 }
191
192 }
193 }
194
195 //
196 // Get fresh mode data from MNP, since underlying media status may change
197 //
198 Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData);
199
200 goto Exit;
201
202 Error:
203 if (Ip6ModeData != NULL) {
204 if (Ip6ModeData->AddressList != NULL) {
205 FreePool (Ip6ModeData->AddressList);
206 }
207
208 if (Ip6ModeData->GroupTable != NULL) {
209 FreePool (Ip6ModeData->GroupTable);
210 }
211
212 if (Ip6ModeData->RouteTable != NULL) {
213 FreePool (Ip6ModeData->RouteTable);
214 }
215
216 if (Ip6ModeData->NeighborCache != NULL) {
217 FreePool (Ip6ModeData->NeighborCache);
218 }
219
220 if (Ip6ModeData->PrefixTable != NULL) {
221 FreePool (Ip6ModeData->PrefixTable);
222 }
223
224 if (Ip6ModeData->IcmpTypeList != NULL) {
225 FreePool (Ip6ModeData->IcmpTypeList);
226 }
227 }
228
229 Exit:
230 gBS->RestoreTPL (OldTpl);
231 return Status;
232 }
233
234 /**
235 Validate that Ipv6 address is OK to be used as station address or next hop address/ neighbor.
236
237 @param[in] IpSb The IP6 service instance.
238 @param[in] Ip The IPv6 address to validate.
239 @param[in] Flag If TRUE, validate if the address is OK to be used
240 as station address. If FALSE, validate if the
241 address is OK to be used as the next hop address/
242 neighbor.
243
244 @retval TRUE The Ip address is valid and could be used.
245 @retval FALSE Invalid Ip address.
246
247 **/
248 BOOLEAN
249 Ip6IsValidAddress (
250 IN IP6_SERVICE *IpSb,
251 IN EFI_IPv6_ADDRESS *Ip,
252 IN BOOLEAN Flag
253 )
254 {
255 if (!NetIp6IsUnspecifiedAddr (Ip)) {
256 if (!NetIp6IsValidUnicast(Ip)) {
257 return FALSE;
258 }
259 if (Ip6IsOneOfSetAddress (IpSb, Ip, NULL, NULL)) {
260 return Flag;
261 }
262 } else {
263 return Flag;
264 }
265
266 return (BOOLEAN) !Flag;
267 }
268
269 /**
270 Validate whether the value of protocol is illegal or not. Protocol is the 'Next Header' field
271 in the last IPv6 extension header, or basic IPv6 header is there's no extension header.
272
273 @param[in] Protocol Default value of 'Next Header'
274
275 @retval TRUE The protocol is illegal.
276 @retval FALSE The protocol is legal.
277
278 **/
279 BOOLEAN
280 Ip6IsIllegalProtocol (
281 IN UINT8 Protocol
282 )
283 {
284 if (Protocol == IP6_HOP_BY_HOP || Protocol == EFI_IP_PROTO_ICMP || Protocol == IP4_PROTO_IGMP) {
285 return TRUE;
286 }
287
288 if (Protocol == 41 || Protocol == 43 || Protocol == 44 || Protocol == 59 || Protocol == 60 || Protocol == 124) {
289 return TRUE;
290 }
291
292 return FALSE;
293 }
294
295 /**
296 Intiialize the IP6_PROTOCOL structure to the unconfigured states.
297
298 @param[in] IpSb The IP6 service instance.
299 @param[in, out] IpInstance The IP6 child instance.
300
301 **/
302 VOID
303 Ip6InitProtocol (
304 IN IP6_SERVICE *IpSb,
305 IN OUT IP6_PROTOCOL *IpInstance
306 )
307 {
308 ASSERT ((IpSb != NULL) && (IpInstance != NULL));
309
310 ZeroMem (IpInstance, sizeof (IP6_PROTOCOL));
311
312 IpInstance->Signature = IP6_PROTOCOL_SIGNATURE;
313 IpInstance->State = IP6_STATE_UNCONFIGED;
314 IpInstance->Service = IpSb;
315 IpInstance->GroupList = NULL;
316 CopyMem (&IpInstance->Ip6Proto, &mEfiIp6ProtocolTemplete, sizeof (EFI_IP6_PROTOCOL));
317
318 NetMapInit (&IpInstance->RxTokens);
319 NetMapInit (&IpInstance->TxTokens);
320 InitializeListHead (&IpInstance->Received);
321 InitializeListHead (&IpInstance->Delivered);
322
323 EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);
324 }
325
326 /**
327 Configure the IP6 child. If the child is already configured,
328 change the configuration parameter. Otherwise, configure it
329 for the first time. The caller should validate the configuration
330 before deliver them to it. It also don't do configure NULL.
331
332 @param[in, out] IpInstance The IP6 child to configure.
333 @param[in] Config The configure data.
334
335 @retval EFI_SUCCESS The IP6 child is successfully configured.
336 @retval EFI_DEVICE_ERROR Failed to free the pending transive or to
337 configure underlying MNP, or other errors.
338 @retval EFI_NO_MAPPING The IP6 child is configured to use the default
339 address, but the default address hasn't been
340 configured. The IP6 child doesn't need to be
341 reconfigured when the default address is configured.
342 @retval EFI_OUT_OF_RESOURCES No more memory space is available.
343 @retval other Other error occurs.
344
345 **/
346 EFI_STATUS
347 Ip6ConfigProtocol (
348 IN OUT IP6_PROTOCOL *IpInstance,
349 IN EFI_IP6_CONFIG_DATA *Config
350 )
351 {
352 IP6_SERVICE *IpSb;
353 IP6_INTERFACE *IpIf;
354 EFI_STATUS Status;
355 EFI_IP6_CONFIG_DATA *Current;
356 IP6_ADDRESS_INFO *AddressInfo;
357 BOOLEAN StationZero;
358 BOOLEAN DestZero;
359 EFI_IPv6_ADDRESS Source;
360 BOOLEAN AddrOk;
361
362 IpSb = IpInstance->Service;
363 Current = &IpInstance->ConfigData;
364
365 //
366 // User is changing packet filters. It must be stopped
367 // before the station address can be changed.
368 //
369 if (IpInstance->State == IP6_STATE_CONFIGED) {
370 //
371 // Cancel all the pending transmit/receive from upper layer
372 //
373 Status = Ip6Cancel (IpInstance, NULL);
374
375 if (EFI_ERROR (Status)) {
376 return EFI_DEVICE_ERROR;
377 }
378
379 CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA));
380 return EFI_SUCCESS;
381 }
382
383 //
384 // Set up the interface.
385 //
386 StationZero = NetIp6IsUnspecifiedAddr (&Config->StationAddress);
387 DestZero = NetIp6IsUnspecifiedAddr (&Config->DestinationAddress);
388
389 if (StationZero && DestZero) {
390 //
391 // StationAddress is still zero.
392 //
393
394 NET_GET_REF (IpSb->DefaultInterface);
395 IpInstance->Interface = IpSb->DefaultInterface;
396 InsertTailList (&IpSb->DefaultInterface->IpInstances, &IpInstance->AddrLink);
397
398 CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA));
399 IpInstance->State = IP6_STATE_CONFIGED;
400
401 return EFI_SUCCESS;
402 }
403
404 if (StationZero && !DestZero) {
405 Status = Ip6SelectSourceAddress (IpSb, &Config->DestinationAddress, &Source);
406 if (EFI_ERROR (Status)) {
407 return Status;
408 }
409 } else {
410 IP6_COPY_ADDRESS (&Source, &Config->StationAddress);
411 }
412
413 AddrOk = Ip6IsOneOfSetAddress (IpSb, &Source, &IpIf, &AddressInfo);
414 if (AddrOk) {
415 if (AddressInfo != NULL) {
416 IpInstance->PrefixLength = AddressInfo->PrefixLength;
417 } else {
418 IpInstance->PrefixLength = IP6_LINK_LOCAL_PREFIX_LENGTH;
419 }
420 } else {
421 //
422 // The specified source address is not one of the addresses IPv6 maintains.
423 //
424 return EFI_INVALID_PARAMETER;
425 }
426
427
428 NET_GET_REF (IpIf);
429 IpInstance->Interface = IpIf;
430 InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);
431
432 CopyMem (Current, Config, sizeof (EFI_IP6_CONFIG_DATA));
433 IP6_COPY_ADDRESS (&Current->StationAddress, &Source);
434 IpInstance->State = IP6_STATE_CONFIGED;
435
436 return EFI_SUCCESS;
437 }
438
439 /**
440 Clean up the IP6 child, and release all the resources used by it.
441
442 @param[in, out] IpInstance The IP6 child to clean up.
443
444 @retval EFI_SUCCESS The IP6 child is cleaned up.
445 @retval EFI_DEVICE_ERROR Some resources failed to be released.
446
447 **/
448 EFI_STATUS
449 Ip6CleanProtocol (
450 IN OUT IP6_PROTOCOL *IpInstance
451 )
452 {
453 if (EFI_ERROR (Ip6Cancel (IpInstance, NULL))) {
454 return EFI_DEVICE_ERROR;
455 }
456
457 if (EFI_ERROR (Ip6Groups (IpInstance, FALSE, NULL))) {
458 return EFI_DEVICE_ERROR;
459 }
460
461 //
462 // Some packets haven't been recycled. It is because either the
463 // user forgets to recycle the packets, or because the callback
464 // hasn't been called. Just leave it alone.
465 //
466 if (!IsListEmpty (&IpInstance->Delivered)) {
467 ;
468 }
469
470 if (IpInstance->Interface != NULL) {
471 RemoveEntryList (&IpInstance->AddrLink);
472 Ip6CleanInterface (IpInstance->Interface, IpInstance);
473 IpInstance->Interface = NULL;
474 }
475
476 if (IpInstance->GroupList != NULL) {
477 FreePool (IpInstance->GroupList);
478 IpInstance->GroupList = NULL;
479 IpInstance->GroupCount = 0;
480 }
481
482 NetMapClean (&IpInstance->TxTokens);
483
484 NetMapClean (&IpInstance->RxTokens);
485
486 return EFI_SUCCESS;
487 }
488
489 /**
490 Configure the MNP parameter used by IP. The IP driver uses one MNP
491 child to transmit/receive frames. By default, it configures MNP
492 to receive unicast/multicast/broadcast. Also, it will enable/disable
493 the promiscuous receive according to whether there is IP child
494 enable that or not. If Force is FALSE, it will iterate through
495 all the IP children to check whether the promiscuous receive
496 setting has been changed. If it hasn't been changed, it won't
497 reconfigure the MNP. If Force is TRUE, the MNP is configured
498 whether that is changed or not.
499
500 @param[in] IpSb The IP6 service instance that is to be changed.
501 @param[in] Force Force the configuration or not.
502
503 @retval EFI_SUCCESS The MNP successfully configured/reconfigured.
504 @retval Others Configuration failed.
505
506 **/
507 EFI_STATUS
508 Ip6ServiceConfigMnp (
509 IN IP6_SERVICE *IpSb,
510 IN BOOLEAN Force
511 )
512 {
513 LIST_ENTRY *Entry;
514 LIST_ENTRY *ProtoEntry;
515 IP6_INTERFACE *IpIf;
516 IP6_PROTOCOL *IpInstance;
517 BOOLEAN Reconfig;
518 BOOLEAN PromiscReceive;
519 EFI_STATUS Status;
520
521 Reconfig = FALSE;
522 PromiscReceive = FALSE;
523
524 if (!Force) {
525 //
526 // Iterate through the IP children to check whether promiscuous
527 // receive setting has been changed. Update the interface's receive
528 // filter also.
529 //
530 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
531
532 IpIf = NET_LIST_USER_STRUCT (Entry, IP6_INTERFACE, Link);
533 IpIf->PromiscRecv = FALSE;
534
535 NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) {
536 IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP6_PROTOCOL, AddrLink);
537
538 if (IpInstance->ConfigData.AcceptPromiscuous) {
539 IpIf->PromiscRecv = TRUE;
540 PromiscReceive = TRUE;
541 }
542 }
543 }
544
545 //
546 // If promiscuous receive isn't changed, it isn't necessary to reconfigure.
547 //
548 if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) {
549 return EFI_SUCCESS;
550 }
551
552 Reconfig = TRUE;
553 IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive;
554 }
555
556 Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData);
557
558 //
559 // recover the original configuration if failed to set the configure.
560 //
561 if (EFI_ERROR (Status) && Reconfig) {
562 IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive;
563 }
564
565 return Status;
566 }
567
568 /**
569 Assigns an IPv6 address and subnet mask to this EFI IPv6 Protocol driver instance.
570
571 The Configure() function is used to set, change, or reset the operational parameters and filter
572 settings for this EFI IPv6 Protocol instance. Until these parameters have been set, no network traffic
573 can be sent or received by this instance. Once the parameters have been reset (by calling this
574 function with Ip6ConfigData set to NULL), no more traffic can be sent or received until these
575 parameters have been set again. Each EFI IPv6 Protocol instance can be started and stopped
576 independently of each other by enabling or disabling their receive filter settings with the
577 Configure() function.
578
579 If Ip6ConfigData.StationAddress is a valid non-zero IPv6 unicast address, it is required
580 to be one of the currently configured IPv6 addresses listed in the EFI IPv6 drivers, or else
581 EFI_INVALID_PARAMETER will be returned. If Ip6ConfigData.StationAddress is
582 unspecified, the IPv6 driver will bind a source address according to the source address selection
583 algorithm. Clients could frequently call GetModeData() to check get currently configured IPv6
584 address list in the EFI IPv6 driver. If both Ip6ConfigData.StationAddress and
585 Ip6ConfigData.Destination are unspecified, when transmitting the packet afterwards, the
586 source address filled in each outgoing IPv6 packet is decided based on the destination of this packet.
587
588 If operational parameters are reset or changed, any pending transmit and receive requests will be
589 cancelled. Their completion token status will be set to EFI_ABORTED and their events will be
590 signaled.
591
592 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
593 @param[in] Ip6ConfigData Pointer to the EFI IPv6 Protocol configuration data structure.
594 If NULL, reset the configuration data.
595
596 @retval EFI_SUCCESS The driver instance was successfully opened.
597 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
598 - This is NULL.
599 - Ip6ConfigData.StationAddress is neither zero nor
600 a unicast IPv6 address.
601 - Ip6ConfigData.StationAddress is neither zero nor
602 one of the configured IP addresses in the EFI IPv6 driver.
603 - Ip6ConfigData.DefaultProtocol is illegal.
604 @retval EFI_OUT_OF_RESOURCES The EFI IPv6 Protocol driver instance data could not be allocated.
605 @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing a source address for
606 this instance, but no source address was available for use.
607 @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the IPv6
608 address or prefix length can be changed.
609 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv6
610 Protocol driver instance was not opened.
611 @retval EFI_UNSUPPORTED Default protocol specified through
612 Ip6ConfigData.DefaulProtocol isn't supported.
613
614 **/
615 EFI_STATUS
616 EFIAPI
617 EfiIp6Configure (
618 IN EFI_IP6_PROTOCOL *This,
619 IN EFI_IP6_CONFIG_DATA *Ip6ConfigData OPTIONAL
620 )
621 {
622 IP6_PROTOCOL *IpInstance;
623 EFI_IP6_CONFIG_DATA *Current;
624 EFI_TPL OldTpl;
625 EFI_STATUS Status;
626 IP6_SERVICE *IpSb;
627
628 //
629 // First, validate the parameters
630 //
631 if (This == NULL) {
632 return EFI_INVALID_PARAMETER;
633 }
634
635 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
636 IpSb = IpInstance->Service;
637
638 if (IpSb->LinkLocalDadFail && Ip6ConfigData != NULL) {
639 return EFI_DEVICE_ERROR;
640 }
641
642 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
643
644 Status = EFI_INVALID_PARAMETER;
645
646 //
647 // Validate the configuration first.
648 //
649 if (Ip6ConfigData != NULL) {
650 //
651 // Check whether the station address is valid.
652 //
653 if (!Ip6IsValidAddress (IpSb, &Ip6ConfigData->StationAddress, TRUE)) {
654 goto Exit;
655 }
656 //
657 // Check whether the default protocol is valid.
658 //
659 if (Ip6IsIllegalProtocol (Ip6ConfigData->DefaultProtocol)) {
660 goto Exit;
661 }
662
663 //
664 // User can only update packet filters when already configured.
665 // If it wants to change the station address, it must configure(NULL)
666 // the instance firstly.
667 //
668 if (IpInstance->State == IP6_STATE_CONFIGED) {
669 Current = &IpInstance->ConfigData;
670
671 if (!EFI_IP6_EQUAL (&Current->StationAddress, &Ip6ConfigData->StationAddress)) {
672 Status = EFI_ALREADY_STARTED;
673 goto Exit;
674 }
675
676 if (NetIp6IsUnspecifiedAddr (&Current->StationAddress) && IP6_NO_MAPPING (IpInstance)) {
677 Status = EFI_NO_MAPPING;
678 goto Exit;
679 }
680 }
681 }
682
683 //
684 // Configure the instance or clean it up.
685 //
686 if (Ip6ConfigData != NULL) {
687 Status = Ip6ConfigProtocol (IpInstance, Ip6ConfigData);
688 } else {
689 Status = Ip6CleanProtocol (IpInstance);
690
691 //
692 // Don't change the state if it is DESTROY, consider the following
693 // valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,
694 // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,
695 // the unload fails miserably.
696 //
697 if (IpInstance->State == IP6_STATE_CONFIGED) {
698 IpInstance->State = IP6_STATE_UNCONFIGED;
699 }
700 }
701
702 //
703 // Update the MNP's configure data. Ip6ServiceConfigMnp will check
704 // whether it is necessary to reconfigure the MNP.
705 //
706 Ip6ServiceConfigMnp (IpInstance->Service, FALSE);
707
708 Exit:
709 gBS->RestoreTPL (OldTpl);
710 return Status;
711 }
712
713 /**
714 Joins and leaves multicast groups.
715
716 The Groups() function is used to join and leave multicast group sessions. Joining a group will
717 enable reception of matching multicast packets. Leaving a group will disable reception of matching
718 multicast packets. Source-Specific Multicast isn't required to be supported.
719
720 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
721
722 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
723 @param[in] JoinFlag Set to TRUE to join the multicast group session, and FALSE to leave.
724 @param[in] GroupAddress Pointer to the IPv6 multicast address.
725 This is an optional parameter that may be NULL.
726
727 @retval EFI_SUCCESS The operation completed successfully.
728 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
729 - This is NULL.
730 - JoinFlag is TRUE and GroupAddress is NULL.
731 - GroupAddress is not NULL and *GroupAddress is
732 not a multicast IPv6 address.
733 - GroupAddress is not NULL and *GroupAddress is in the
734 range of SSM destination address.
735 @retval EFI_NOT_STARTED This instance has not been started.
736 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
737 @retval EFI_UNSUPPORTED This EFI IPv6 Protocol implementation does not support multicast groups.
738 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
739 JoinFlag is TRUE).
740 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
741 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
742
743 **/
744 EFI_STATUS
745 EFIAPI
746 EfiIp6Groups (
747 IN EFI_IP6_PROTOCOL *This,
748 IN BOOLEAN JoinFlag,
749 IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL
750 )
751 {
752 EFI_TPL OldTpl;
753 EFI_STATUS Status;
754 IP6_PROTOCOL *IpInstance;
755 IP6_SERVICE *IpSb;
756
757 if ((This == NULL) || (JoinFlag && GroupAddress == NULL)) {
758 return EFI_INVALID_PARAMETER;
759 }
760
761 if (GroupAddress != NULL && !IP6_IS_MULTICAST (GroupAddress)) {
762 return EFI_INVALID_PARAMETER;
763 }
764
765 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
766 IpSb = IpInstance->Service;
767
768 if (IpSb->LinkLocalDadFail) {
769 return EFI_DEVICE_ERROR;
770 }
771
772 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
773
774 if (IpInstance->State != IP6_STATE_CONFIGED) {
775 Status = EFI_NOT_STARTED;
776 goto ON_EXIT;
777 }
778
779 Status = Ip6Groups (IpInstance, JoinFlag, GroupAddress);
780
781 ON_EXIT:
782 gBS->RestoreTPL (OldTpl);
783 return Status;
784 }
785
786 /**
787 Adds and deletes routing table entries.
788
789 The Routes() function adds a route to, or deletes a route from, the routing table.
790
791 Routes are determined by comparing the leftmost PrefixLength bits of Destination with
792 the destination IPv6 address arithmetically. The gateway address must be on the same subnet as the
793 configured station address.
794
795 The default route is added with Destination and PrefixLegth both set to all zeros. The
796 default route matches all destination IPv6 addresses that do not match any other routes.
797
798 All EFI IPv6 Protocol instances share a routing table.
799
800 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
801 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
802 FALSE to add this route to the routing table. Destination,
803 PrefixLength and Gateway are used as the key to each
804 route entry.
805 @param[in] Destination The address prefix of the subnet that needs to be routed.
806 This is an optional parameter that may be NULL.
807 @param[in] PrefixLength The prefix length of Destination. Ignored if Destination
808 is NULL.
809 @param[in] GatewayAddress The unicast gateway IPv6 address for this route.
810 This is an optional parameter that may be NULL.
811
812 @retval EFI_SUCCESS The operation completed successfully.
813 @retval EFI_NOT_STARTED The driver instance has not been started.
814 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
815 - This is NULL.
816 - When DeleteRoute is TRUE, both Destination and
817 GatewayAddress are NULL.
818 - When DeleteRoute is FALSE, either Destination or
819 GatewayAddress is NULL.
820 - *GatewayAddress is not a valid unicast IPv6 address.
821 - *GatewayAddress is one of the local configured IPv6
822 addresses.
823 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
824 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
825 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
826 DeleteRoute is FALSE).
827
828 **/
829 EFI_STATUS
830 EFIAPI
831 EfiIp6Routes (
832 IN EFI_IP6_PROTOCOL *This,
833 IN BOOLEAN DeleteRoute,
834 IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
835 IN UINT8 PrefixLength,
836 IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL
837 )
838 {
839 IP6_PROTOCOL *IpInstance;
840 EFI_STATUS Status;
841 EFI_TPL OldTpl;
842 IP6_SERVICE *IpSb;
843
844 if ((This == NULL) || (PrefixLength >= IP6_PREFIX_NUM)) {
845 return EFI_INVALID_PARAMETER;
846 }
847
848 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
849 IpSb = IpInstance->Service;
850
851 if (IpSb->LinkLocalDadFail) {
852 return EFI_DEVICE_ERROR;
853 }
854
855 if (IpInstance->State != IP6_STATE_CONFIGED) {
856 return EFI_NOT_STARTED;
857 }
858
859 if (DeleteRoute && (Destination == NULL) && (GatewayAddress == NULL)) {
860 return EFI_INVALID_PARAMETER;
861 }
862
863 if (!DeleteRoute && (Destination == NULL || GatewayAddress == NULL)) {
864 return EFI_INVALID_PARAMETER;
865 }
866
867 if (GatewayAddress != NULL) {
868 if (!Ip6IsValidAddress (IpSb, GatewayAddress, FALSE)) {
869 return EFI_INVALID_PARAMETER;
870 }
871
872 if (!NetIp6IsUnspecifiedAddr (GatewayAddress) &&
873 !NetIp6IsNetEqual (GatewayAddress, &IpInstance->ConfigData.StationAddress, PrefixLength)
874 ) {
875 return EFI_INVALID_PARAMETER;
876 }
877 }
878
879 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
880
881 //
882 // Update the route table
883 //
884 if (DeleteRoute) {
885 Status = Ip6DelRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress);
886 } else {
887 Status = Ip6AddRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress);
888 }
889
890 gBS->RestoreTPL (OldTpl);
891 return Status;
892 }
893
894 /**
895 Add or delete Neighbor cache entries.
896
897 The Neighbors() function is used to add, update, or delete an entry from neighbor cache.
898 IPv6 neighbor cache entries are typically inserted and updated by the network protocol driver as
899 network traffic is processed. Most neighbor cache entries will timeout and be deleted if the network
900 traffic stops. Neighbor cache entries that were inserted by Neighbors() may be static (will not
901 timeout) or dynamic (will timeout).
902
903 The implementation should follow the neighbor cache timeout mechanism which is defined in
904 RFC4861. The default neighbor cache timeout value should be tuned for the expected network
905 environment
906
907 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
908 @param[in] DeleteFlag Set to TRUE to delete the specified cache entry, set to FALSE to
909 add (or update, if it already exists and Override is TRUE) the
910 specified cache entry. TargetIp6Address is used as the key
911 to find the requested cache entry.
912 @param[in] TargetIp6Address Pointer to the Target IPv6 address.
913 @param[in] TargetLinkAddress Pointer to the link-layer address of the target. Ignored if NULL.
914 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
915 cache, it will be deleted after Timeout. A value of zero means that
916 the entry is permanent. A non-zero value means that the entry is
917 dynamic.
918 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
919 be overridden and updated; if FALSE, EFI_ACCESS_DENIED
920 will be returned if a corresponding cache entry already existed.
921
922 @retval EFI_SUCCESS The data has been queued for transmission.
923 @retval EFI_NOT_STARTED This instance has not been started.
924 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
925 - This is NULL.
926 - TargetIpAddress is NULL.
927 - *TargetLinkAddress is invalid when not NULL.
928 - *TargetIpAddress is not a valid unicast IPv6 address.
929 - *TargetIpAddress is one of the local configured IPv6
930 addresses.
931 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache.
932 @retval EFI_NOT_FOUND This entry is not in the neighbor cache (when DeleteFlag is
933 TRUE or when DeleteFlag is FALSE while
934 TargetLinkAddress is NULL.).
935 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
936 and that entry is tagged as un-overridden (when Override
937 is FALSE).
938
939 **/
940 EFI_STATUS
941 EFIAPI
942 EfiIp6Neighbors (
943 IN EFI_IP6_PROTOCOL *This,
944 IN BOOLEAN DeleteFlag,
945 IN EFI_IPv6_ADDRESS *TargetIp6Address,
946 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
947 IN UINT32 Timeout,
948 IN BOOLEAN Override
949 )
950 {
951 EFI_TPL OldTpl;
952 EFI_STATUS Status;
953 IP6_PROTOCOL *IpInstance;
954 IP6_SERVICE *IpSb;
955
956 if (This == NULL || TargetIp6Address == NULL) {
957 return EFI_INVALID_PARAMETER;
958 }
959
960 if (NetIp6IsUnspecifiedAddr (TargetIp6Address)) {
961 return EFI_INVALID_PARAMETER;
962 }
963
964 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
965 IpSb = IpInstance->Service;
966
967 if (IpSb->LinkLocalDadFail) {
968 return EFI_DEVICE_ERROR;
969 }
970
971 if (!Ip6IsValidAddress (IpSb, TargetIp6Address, FALSE)) {
972 return EFI_INVALID_PARAMETER;
973 }
974
975 if (TargetLinkAddress != NULL) {
976 if (!Ip6IsValidLinkAddress (IpSb, TargetLinkAddress)) {
977 return EFI_INVALID_PARAMETER;
978 }
979 }
980
981 if (Ip6IsOneOfSetAddress (IpSb, TargetIp6Address, NULL, NULL)) {
982 return EFI_INVALID_PARAMETER;
983 }
984
985 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
986 if (IpInstance->State != IP6_STATE_CONFIGED) {
987 Status = EFI_NOT_STARTED;
988 goto Exit;
989 }
990
991 if (DeleteFlag) {
992 Status = Ip6DelNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override);
993 } else {
994 Status = Ip6AddNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override);
995 }
996
997 Exit:
998 gBS->RestoreTPL (OldTpl);
999 return Status;
1000 }
1001
1002 /**
1003 Check whether the user's token or event has already
1004 been enqueue on IP6's list.
1005
1006 @param[in] Map The container of either user's transmit or receive
1007 token.
1008 @param[in] Item Current item to check against.
1009 @param[in] Context The Token to check againist.
1010
1011 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
1012 @retval EFI_SUCCESS The current item isn't the same token/event as the
1013 context.
1014
1015 **/
1016 EFI_STATUS
1017 EFIAPI
1018 Ip6TokenExist (
1019 IN NET_MAP *Map,
1020 IN NET_MAP_ITEM *Item,
1021 IN VOID *Context
1022 )
1023 {
1024 EFI_IP6_COMPLETION_TOKEN *Token;
1025 EFI_IP6_COMPLETION_TOKEN *TokenInItem;
1026
1027 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1028 TokenInItem = (EFI_IP6_COMPLETION_TOKEN *) Item->Key;
1029
1030 if (Token == TokenInItem || Token->Event == TokenInItem->Event) {
1031 return EFI_ACCESS_DENIED;
1032 }
1033
1034 return EFI_SUCCESS;
1035 }
1036
1037 /**
1038 Validate the user's token against the current station address.
1039
1040 @param[in] Token User's token to validate.
1041
1042 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1043 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.
1044 @retval EFI_SUCCESS The token is OK.
1045
1046 **/
1047 EFI_STATUS
1048 Ip6TxTokenValid (
1049 IN EFI_IP6_COMPLETION_TOKEN *Token
1050 )
1051 {
1052 EFI_IP6_TRANSMIT_DATA *TxData;
1053 UINT32 Index;
1054 UINT32 DataLength;
1055
1056 if (Token == NULL || Token->Event == NULL) {
1057 return EFI_INVALID_PARAMETER;
1058 }
1059
1060 TxData = Token->Packet.TxData;
1061
1062 if (TxData == NULL || (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs == NULL)) {
1063 return EFI_INVALID_PARAMETER;
1064 }
1065
1066 if (TxData->FragmentCount == 0 || TxData->DataLength == 0) {
1067 return EFI_INVALID_PARAMETER;
1068 }
1069
1070 for (DataLength = 0, Index = 0; Index < TxData->FragmentCount; Index++) {
1071 if (TxData->FragmentTable[Index].FragmentLength == 0 || TxData->FragmentTable[Index].FragmentBuffer == NULL) {
1072 return EFI_INVALID_PARAMETER;
1073 }
1074
1075 DataLength += TxData->FragmentTable[Index].FragmentLength;
1076 }
1077
1078 if (TxData->DataLength != DataLength) {
1079 return EFI_INVALID_PARAMETER;
1080 }
1081
1082 //
1083 // TODO: Token.Packet.TxData.DataLength is too short to transmit.
1084 // return EFI_BUFFER_TOO_SMALL;
1085 //
1086
1087 //
1088 // If Token.Packet.TxData.DataLength is beyond the maximum that which can be
1089 // described through the Fragment Offset field in Fragment header when performing
1090 // fragmentation.
1091 //
1092 if (TxData->DataLength > 64 * 1024) {
1093 return EFI_BAD_BUFFER_SIZE;
1094 }
1095
1096 return EFI_SUCCESS;
1097 }
1098
1099 /**
1100 The callback function for the net buffer which wraps the user's
1101 transmit token. Although this function seems simple, there
1102 are some subtle aspects.
1103 When user requests the IP to transmit a packet by passing it a
1104 token, the token is wrapped in an IP6_TXTOKEN_WRAP and the data
1105 is wrapped in an net buffer. The net buffer's Free function is
1106 set to Ip6FreeTxToken. The Token and token wrap are added to the
1107 IP child's TxToken map. Then the buffer is passed to Ip6Output for
1108 transmission. If an error happened before that, the buffer
1109 is freed, which in turn frees the token wrap. The wrap may
1110 have been added to the TxToken map or not, and the user's event
1111 shouldn't be fired because we are still in the EfiIp6Transmit. If
1112 the buffer has been sent by Ip6Output, it should be removed from
1113 the TxToken map and user's event signaled. The token wrap and buffer
1114 are bound together. Check the comments in Ip6Output for information
1115 about IP fragmentation.
1116
1117 @param[in] Context The token's wrap.
1118
1119 **/
1120 VOID
1121 EFIAPI
1122 Ip6FreeTxToken (
1123 IN VOID *Context
1124 )
1125 {
1126 IP6_TXTOKEN_WRAP *Wrap;
1127 NET_MAP_ITEM *Item;
1128
1129 Wrap = (IP6_TXTOKEN_WRAP *) Context;
1130
1131 //
1132 // Signal IpSecRecycleEvent to inform IPsec free the memory
1133 //
1134 if (Wrap->IpSecRecycleSignal != NULL) {
1135 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
1136 }
1137
1138 //
1139 // Find the token in the instance's map. EfiIp6Transmit put the
1140 // token to the map. If that failed, NetMapFindKey will return NULL.
1141 //
1142 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);
1143
1144 if (Item != NULL) {
1145 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);
1146 }
1147
1148 if (Wrap->Sent) {
1149 gBS->SignalEvent (Wrap->Token->Event);
1150
1151 //
1152 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
1153 //
1154 DispatchDpc ();
1155 }
1156
1157 FreePool (Wrap);
1158 }
1159
1160
1161 /**
1162 The callback function to Ip6Output to update the transmit status.
1163
1164 @param[in] Packet The user's transmit packet.
1165 @param[in] IoStatus The result of the transmission.
1166 @param[in] Flag Not used during transmission.
1167 @param[in] Context The token's wrap.
1168
1169 **/
1170 VOID
1171 Ip6OnPacketSent (
1172 IN NET_BUF *Packet,
1173 IN EFI_STATUS IoStatus,
1174 IN UINT32 Flag,
1175 IN VOID *Context
1176 )
1177 {
1178 IP6_TXTOKEN_WRAP *Wrap;
1179
1180 //
1181 // This is the transmission request from upper layer,
1182 // not the IP6 driver itself.
1183 //
1184 Wrap = (IP6_TXTOKEN_WRAP *) Context;
1185 Wrap->Token->Status = IoStatus;
1186
1187 NetbufFree (Wrap->Packet);
1188 }
1189
1190 /**
1191 Places outgoing data packets into the transmit queue.
1192
1193 The Transmit() function places a sending request in the transmit queue of this
1194 EFI IPv6 Protocol instance. Whenever the packet in the token is sent out or some
1195 errors occur, the event in the token will be signaled, and the status is updated.
1196
1197 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1198 @param[in] Token Pointer to the transmit token.
1199
1200 @retval EFI_SUCCESS The data has been queued for transmission.
1201 @retval EFI_NOT_STARTED This instance has not been started.
1202 @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing
1203 a source address for this transmission,
1204 but no source address was available for use.
1205 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
1206 - This is NULL.
1207 - Token is NULL.
1208 - Token.Event is NULL.
1209 - Token.Packet.TxData is NULL.
1210 - Token.Packet.ExtHdrsLength is not zero and
1211 Token.Packet.ExtHdrs is NULL.
1212 - Token.Packet.FragmentCount is zero.
1213 - One or more of the Token.Packet.TxData.
1214 FragmentTable[].FragmentLength fields is zero.
1215 - One or more of the Token.Packet.TxData.
1216 FragmentTable[].FragmentBuffer fields is NULL.
1217 - Token.Packet.TxData.DataLength is zero or not
1218 equal to the sum of fragment lengths.
1219 - Token.Packet.TxData.DestinationAddress is non
1220 zero when DestinationAddress is configured as
1221 non-zero when doing Configure() for this
1222 EFI IPv6 protocol instance.
1223 - Token.Packet.TxData.DestinationAddress is
1224 unspecified when DestinationAddress is unspecified
1225 when doing Configure() for this EFI IPv6 protocol
1226 instance.
1227 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.
1228 Event was already in the transmit queue.
1229 @retval EFI_NOT_READY The completion token could not be queued because
1230 the transmit queue is full.
1231 @retval EFI_NOT_FOUND Not route is found to destination address.
1232 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
1233 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
1234 short to transmit.
1235 @retval EFI_BAD_BUFFER_SIZE If Token.Packet.TxData.DataLength is beyond the
1236 maximum that which can be described through the
1237 Fragment Offset field in Fragment header when
1238 performing fragmentation.
1239 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1240
1241 **/
1242 EFI_STATUS
1243 EFIAPI
1244 EfiIp6Transmit (
1245 IN EFI_IP6_PROTOCOL *This,
1246 IN EFI_IP6_COMPLETION_TOKEN *Token
1247 )
1248 {
1249 IP6_SERVICE *IpSb;
1250 IP6_PROTOCOL *IpInstance;
1251 EFI_IP6_CONFIG_DATA *Config;
1252 EFI_STATUS Status;
1253 EFI_TPL OldTpl;
1254 EFI_IP6_HEADER Head;
1255 EFI_IP6_TRANSMIT_DATA *TxData;
1256 EFI_IP6_OVERRIDE_DATA *Override;
1257 IP6_TXTOKEN_WRAP *Wrap;
1258 UINT8 *ExtHdrs;
1259
1260 //
1261 // Check input parameters.
1262 //
1263 if (This == NULL) {
1264 return EFI_INVALID_PARAMETER;
1265 }
1266
1267 ExtHdrs = NULL;
1268
1269 Status = Ip6TxTokenValid (Token);
1270 if (EFI_ERROR (Status)) {
1271 return Status;
1272 }
1273
1274 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1275 IpSb = IpInstance->Service;
1276
1277 if (IpSb->LinkLocalDadFail) {
1278 return EFI_DEVICE_ERROR;
1279 }
1280
1281 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1282
1283 if (IpInstance->State != IP6_STATE_CONFIGED) {
1284 Status = EFI_NOT_STARTED;
1285 goto Exit;
1286 }
1287
1288 Config = &IpInstance->ConfigData;
1289
1290 //
1291 // Check whether the token or signal already existed.
1292 //
1293 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip6TokenExist, Token))) {
1294 Status = EFI_ACCESS_DENIED;
1295 goto Exit;
1296 }
1297
1298 //
1299 // Build the IP header, fill in the information from ConfigData or OverrideData
1300 //
1301 ZeroMem (&Head, sizeof(EFI_IP6_HEADER));
1302 TxData = Token->Packet.TxData;
1303 IP6_COPY_ADDRESS (&Head.SourceAddress, &Config->StationAddress);
1304 IP6_COPY_ADDRESS (&Head.DestinationAddress, &Config->DestinationAddress);
1305
1306 Status = EFI_INVALID_PARAMETER;
1307
1308 if (NetIp6IsUnspecifiedAddr (&TxData->DestinationAddress)) {
1309 if (NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) {
1310 goto Exit;
1311 }
1312
1313 ASSERT (!NetIp6IsUnspecifiedAddr (&Config->StationAddress));
1314
1315 } else {
1316 //
1317 // StationAddress is unspecified only when ConfigData.Dest is unspecified.
1318 // Use TxData.Dest to override the DestinationAddress.
1319 //
1320 if (!NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) {
1321 goto Exit;
1322 }
1323
1324 if (NetIp6IsUnspecifiedAddr (&Config->StationAddress)) {
1325 Status = Ip6SelectSourceAddress (
1326 IpSb,
1327 &TxData->DestinationAddress,
1328 &Head.SourceAddress
1329 );
1330 if (EFI_ERROR (Status)) {
1331 goto Exit;
1332 }
1333 }
1334
1335 IP6_COPY_ADDRESS (&Head.DestinationAddress, &TxData->DestinationAddress);
1336 }
1337
1338 //
1339 // Fill in Head infos.
1340 //
1341 Head.NextHeader = Config->DefaultProtocol;
1342 if (TxData->ExtHdrsLength != 0) {
1343 Head.NextHeader = TxData->NextHeader;
1344 }
1345
1346 if (TxData->OverrideData != NULL) {
1347 Override = TxData->OverrideData;
1348 Head.NextHeader = Override->Protocol;
1349 Head.HopLimit = Override->HopLimit;
1350 Head.FlowLabelL = HTONS ((UINT16) Override->FlowLabel);
1351 Head.FlowLabelH = (UINT8) ((Override->FlowLabel >> 16) & 0x0F);
1352 } else {
1353 Head.HopLimit = Config->HopLimit;
1354 Head.FlowLabelL = HTONS ((UINT16) Config->FlowLabel);
1355 Head.FlowLabelH = (UINT8) ((Config->FlowLabel >> 16) & 0x0F);
1356 }
1357
1358 Head.PayloadLength = HTONS ((UINT16) (TxData->ExtHdrsLength + TxData->DataLength));
1359
1360 //
1361 // OK, it survives all the validation check. Wrap the token in
1362 // a IP6_TXTOKEN_WRAP and the data in a netbuf
1363 //
1364 Status = EFI_OUT_OF_RESOURCES;
1365 Wrap = AllocateZeroPool (sizeof (IP6_TXTOKEN_WRAP));
1366 if (Wrap == NULL) {
1367 goto Exit;
1368 }
1369
1370 Wrap->IpInstance = IpInstance;
1371 Wrap->Token = Token;
1372 Wrap->Sent = FALSE;
1373 Wrap->Life = IP6_US_TO_SEC (Config->TransmitTimeout);
1374 Wrap->Packet = NetbufFromExt (
1375 (NET_FRAGMENT *) TxData->FragmentTable,
1376 TxData->FragmentCount,
1377 IP6_MAX_HEADLEN,
1378 0,
1379 Ip6FreeTxToken,
1380 Wrap
1381 );
1382
1383 if (Wrap->Packet == NULL) {
1384 FreePool (Wrap);
1385 goto Exit;
1386 }
1387
1388 Token->Status = EFI_NOT_READY;
1389
1390 Status = NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap);
1391 if (EFI_ERROR (Status)) {
1392 //
1393 // NetbufFree will call Ip6FreeTxToken, which in turn will
1394 // free the IP6_TXTOKEN_WRAP. Now, the token wrap hasn't been
1395 // enqueued.
1396 //
1397 NetbufFree (Wrap->Packet);
1398 goto Exit;
1399 }
1400
1401 //
1402 // Allocate a new buffer to store IPv6 extension headers to avoid updating
1403 // the original data in EFI_IP6_COMPLETION_TOKEN.
1404 //
1405 if (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs != NULL) {
1406 ExtHdrs = (UINT8 *) AllocateCopyPool (TxData->ExtHdrsLength, TxData->ExtHdrs);
1407 if (ExtHdrs == NULL) {
1408 Status = EFI_OUT_OF_RESOURCES;
1409 goto Exit;
1410 }
1411 }
1412
1413 //
1414 // Mark the packet sent before output it. Mark it not sent again if the
1415 // returned status is not EFI_SUCCESS;
1416 //
1417 Wrap->Sent = TRUE;
1418
1419 Status = Ip6Output (
1420 IpSb,
1421 NULL,
1422 IpInstance,
1423 Wrap->Packet,
1424 &Head,
1425 ExtHdrs,
1426 TxData->ExtHdrsLength,
1427 Ip6OnPacketSent,
1428 Wrap
1429 );
1430 if (EFI_ERROR (Status)) {
1431 Wrap->Sent = FALSE;
1432 NetbufFree (Wrap->Packet);
1433 }
1434
1435 Exit:
1436 gBS->RestoreTPL (OldTpl);
1437
1438 if (ExtHdrs != NULL) {
1439 FreePool (ExtHdrs);
1440 }
1441
1442 return Status;
1443 }
1444
1445 /**
1446 Places a receiving request into the receiving queue.
1447
1448 The Receive() function places a completion token into the receive packet queue.
1449 This function is always asynchronous.
1450
1451 The Token.Event field in the completion token must be filled in by the caller
1452 and cannot be NULL. When the receive operation completes, the EFI IPv6 Protocol
1453 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
1454 is signaled.
1455
1456 Current Udp implementation creates an IP child for each Udp child.
1457 It initates a asynchronous receive immediately no matter whether
1458 there is no mapping or not. Therefore, disable the returning EFI_NO_MAPPING for now.
1459 To enable it, the following check must be performed:
1460
1461 if (NetIp6IsUnspecifiedAddr (&Config->StationAddress) && IP6_NO_MAPPING (IpInstance)) {
1462 Status = EFI_NO_MAPPING;
1463 goto Exit;
1464 }
1465
1466 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1467 @param[in] Token Pointer to a token that is associated with the receive data descriptor.
1468
1469 @retval EFI_SUCCESS The receive completion token was cached.
1470 @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started.
1471 @retval EFI_NO_MAPPING When IP6 driver responsible for binding source address to this instance,
1472 while no source address is available for use.
1473 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1474 - This is NULL.
1475 - Token is NULL.
1476 - Token.Event is NULL.
1477 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
1478 resources (usually memory).
1479 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1480 The EFI IPv6 Protocol instance has been reset to startup defaults.
1481 @retval EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
1482 in the receive queue.
1483 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
1484
1485 **/
1486 EFI_STATUS
1487 EFIAPI
1488 EfiIp6Receive (
1489 IN EFI_IP6_PROTOCOL *This,
1490 IN EFI_IP6_COMPLETION_TOKEN *Token
1491 )
1492 {
1493 IP6_PROTOCOL *IpInstance;
1494 EFI_STATUS Status;
1495 EFI_TPL OldTpl;
1496 IP6_SERVICE *IpSb;
1497
1498 if (This == NULL || Token == NULL || Token->Event == NULL) {
1499 return EFI_INVALID_PARAMETER;
1500 }
1501
1502 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1503 IpSb = IpInstance->Service;
1504
1505 if (IpSb->LinkLocalDadFail) {
1506 return EFI_DEVICE_ERROR;
1507 }
1508
1509 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1510
1511 if (IpInstance->State != IP6_STATE_CONFIGED) {
1512 Status = EFI_NOT_STARTED;
1513 goto Exit;
1514 }
1515
1516 //
1517 // Check whether the toke is already on the receive queue.
1518 //
1519 Status = NetMapIterate (&IpInstance->RxTokens, Ip6TokenExist, Token);
1520
1521 if (EFI_ERROR (Status)) {
1522 Status = EFI_ACCESS_DENIED;
1523 goto Exit;
1524 }
1525
1526 //
1527 // Queue the token then check whether there is pending received packet.
1528 //
1529 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);
1530
1531 if (EFI_ERROR (Status)) {
1532 goto Exit;
1533 }
1534
1535 Status = Ip6InstanceDeliverPacket (IpInstance);
1536
1537 //
1538 // Dispatch the DPC queued by the NotifyFunction of this instane's receive
1539 // event.
1540 //
1541 DispatchDpc ();
1542
1543 Exit:
1544 gBS->RestoreTPL (OldTpl);
1545 return Status;
1546 }
1547
1548
1549 /**
1550 Cancel the transmitted but not recycled packet. If a matching
1551 token is found, it will call Ip6CancelPacket to cancel the
1552 packet. Ip6CancelPacket cancels all the fragments of the
1553 packet. When all the fragments are freed, the IP6_TXTOKEN_WRAP
1554 is deleted from the Map, and user's event is signalled.
1555 Because Ip6CancelPacket and other functions are all called in
1556 line, after Ip6CancelPacket returns, the Item has been freed.
1557
1558 @param[in] Map The IP6 child's transmit queue.
1559 @param[in] Item The current transmitted packet to test.
1560 @param[in] Context The user's token to cancel.
1561
1562 @retval EFI_SUCCESS Continue to check the next Item.
1563 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
1564
1565 **/
1566 EFI_STATUS
1567 EFIAPI
1568 Ip6CancelTxTokens (
1569 IN NET_MAP *Map,
1570 IN NET_MAP_ITEM *Item,
1571 IN VOID *Context
1572 )
1573 {
1574 EFI_IP6_COMPLETION_TOKEN *Token;
1575 IP6_TXTOKEN_WRAP *Wrap;
1576
1577 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1578
1579 //
1580 // Return EFI_SUCCESS to check the next item in the map if
1581 // this one doesn't match.
1582 //
1583 if ((Token != NULL) && (Token != Item->Key)) {
1584 return EFI_SUCCESS;
1585 }
1586
1587 Wrap = (IP6_TXTOKEN_WRAP *) Item->Value;
1588 ASSERT (Wrap != NULL);
1589
1590 //
1591 // Don't access the Item, Wrap and Token's members after this point.
1592 // Item and wrap has been freed. And we no longer own the Token.
1593 //
1594 Ip6CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
1595
1596 //
1597 // If only one item is to be cancel, return EFI_ABORTED to stop
1598 // iterating the map any more.
1599 //
1600 if (Token != NULL) {
1601 return EFI_ABORTED;
1602 }
1603
1604 return EFI_SUCCESS;
1605 }
1606
1607
1608 /**
1609 Cancel the receive request. This is simple, because
1610 it is only enqueued in our local receive map.
1611
1612 @param[in] Map The IP6 child's receive queue.
1613 @param[in] Item Current receive request to cancel.
1614 @param[in] Context The user's token to cancel.
1615
1616
1617 @retval EFI_SUCCESS Continue to check the next receive request on the
1618 queue.
1619 @retval EFI_ABORTED The user's token (token != NULL) has been
1620 cancelled.
1621
1622 **/
1623 EFI_STATUS
1624 EFIAPI
1625 Ip6CancelRxTokens (
1626 IN NET_MAP *Map,
1627 IN NET_MAP_ITEM *Item,
1628 IN VOID *Context
1629 )
1630 {
1631 EFI_IP6_COMPLETION_TOKEN *Token;
1632 EFI_IP6_COMPLETION_TOKEN *This;
1633
1634 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1635 This = Item->Key;
1636
1637 if ((Token != NULL) && (Token != This)) {
1638 return EFI_SUCCESS;
1639 }
1640
1641 NetMapRemoveItem (Map, Item, NULL);
1642
1643 This->Status = EFI_ABORTED;
1644 This->Packet.RxData = NULL;
1645 gBS->SignalEvent (This->Event);
1646
1647 if (Token != NULL) {
1648 return EFI_ABORTED;
1649 }
1650
1651 return EFI_SUCCESS;
1652 }
1653
1654 /**
1655 Cancel the user's receive/transmit request. It is the worker function of
1656 EfiIp6Cancel API.
1657
1658 @param[in] IpInstance The IP6 child.
1659 @param[in] Token The token to cancel. If NULL, all token will be
1660 cancelled.
1661
1662 @retval EFI_SUCCESS The token is cancelled.
1663 @retval EFI_NOT_FOUND The token isn't found on either the
1664 transmit/receive queue.
1665 @retval EFI_DEVICE_ERROR Not all tokens are cancelled when Token is NULL.
1666
1667 **/
1668 EFI_STATUS
1669 Ip6Cancel (
1670 IN IP6_PROTOCOL *IpInstance,
1671 IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL
1672 )
1673 {
1674 EFI_STATUS Status;
1675
1676 //
1677 // First check the transmitted packet. Ip6CancelTxTokens returns
1678 // EFI_ABORTED to mean that the token has been cancelled when
1679 // token != NULL. So, return EFI_SUCCESS for this condition.
1680 //
1681 Status = NetMapIterate (&IpInstance->TxTokens, Ip6CancelTxTokens, Token);
1682 if (EFI_ERROR (Status)) {
1683 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1684 return EFI_SUCCESS;
1685 }
1686
1687 return Status;
1688 }
1689
1690 //
1691 // Check the receive queue. Ip6CancelRxTokens also returns EFI_ABORT
1692 // for Token!=NULL and it is cancelled.
1693 //
1694 Status = NetMapIterate (&IpInstance->RxTokens, Ip6CancelRxTokens, Token);
1695 //
1696 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
1697 // events.
1698 //
1699 DispatchDpc ();
1700 if (EFI_ERROR (Status)) {
1701 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1702 return EFI_SUCCESS;
1703 }
1704
1705 return Status;
1706 }
1707
1708 //
1709 // OK, if the Token is found when Token != NULL, the NetMapIterate
1710 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.
1711 //
1712 if (Token != NULL) {
1713 return EFI_NOT_FOUND;
1714 }
1715
1716 //
1717 // If Token == NULL, cancel all the tokens. return error if not
1718 // all of them are cancelled.
1719 //
1720 if (!NetMapIsEmpty (&IpInstance->TxTokens) || !NetMapIsEmpty (&IpInstance->RxTokens)) {
1721
1722 return EFI_DEVICE_ERROR;
1723 }
1724
1725 return EFI_SUCCESS;
1726 }
1727
1728 /**
1729 Abort an asynchronous transmit or receive request.
1730
1731 The Cancel() function is used to abort a pending transmit or receive request.
1732 If the token is in the transmit or receive request queues, after calling this
1733 function, Token->Status will be set to EFI_ABORTED, and then Token->Event will
1734 be signaled. If the token is not in one of the queues, which usually means the
1735 asynchronous operation has completed, this function will not signal the token,
1736 and EFI_NOT_FOUND is returned.
1737
1738 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1739 @param[in] Token Pointer to a token that has been issued by
1740 EFI_IP6_PROTOCOL.Transmit() or
1741 EFI_IP6_PROTOCOL.Receive(). If NULL, all pending
1742 tokens are aborted. Type EFI_IP6_COMPLETION_TOKEN is
1743 defined in EFI_IP6_PROTOCOL.Transmit().
1744
1745 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
1746 Token->Event was signaled. When Token is NULL, all
1747 pending requests were aborted, and their events were signaled.
1748 @retval EFI_INVALID_PARAMETER This is NULL.
1749 @retval EFI_NOT_STARTED This instance has not been started.
1750 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
1751 not found in the transmit or receive queue. It has either completed
1752 or was not issued by Transmit() and Receive().
1753 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1754
1755 **/
1756 EFI_STATUS
1757 EFIAPI
1758 EfiIp6Cancel (
1759 IN EFI_IP6_PROTOCOL *This,
1760 IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL
1761 )
1762 {
1763 IP6_PROTOCOL *IpInstance;
1764 IP6_SERVICE *IpSb;
1765 EFI_STATUS Status;
1766 EFI_TPL OldTpl;
1767
1768 if (This == NULL) {
1769 return EFI_INVALID_PARAMETER;
1770 }
1771
1772 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1773 IpSb = IpInstance->Service;
1774
1775 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1776
1777 if (IpInstance->State != IP6_STATE_CONFIGED) {
1778 Status = EFI_NOT_STARTED;
1779 goto Exit;
1780 }
1781
1782 Status = Ip6Cancel (IpInstance, Token);
1783
1784 Exit:
1785 gBS->RestoreTPL (OldTpl);
1786 return Status;
1787 }
1788
1789 /**
1790 Polls for incoming data packets, and processes outgoing data packets.
1791
1792 The Poll() function polls for incoming data packets and processes outgoing data
1793 packets. Network drivers and applications can call the EFI_IP6_PROTOCOL.Poll()
1794 function to increase the rate that data packets are moved between the communications
1795 device and the transmit and receive queues.
1796
1797 In some systems the periodic timer event may not poll the underlying communications
1798 device fast enough to transmit and/or receive all data packets without missing
1799 incoming packets or dropping outgoing packets. Drivers and applications that are
1800 experiencing packet loss should try calling the EFI_IP6_PROTOCOL.Poll() function
1801 more often.
1802
1803 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1804
1805 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1806 @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started.
1807 @retval EFI_INVALID_PARAMETER This is NULL.
1808 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.
1809 @retval EFI_NOT_READY No incoming or outgoing data was processed.
1810 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
1811 Consider increasing the polling rate.
1812
1813 **/
1814 EFI_STATUS
1815 EFIAPI
1816 EfiIp6Poll (
1817 IN EFI_IP6_PROTOCOL *This
1818 )
1819 {
1820 IP6_PROTOCOL *IpInstance;
1821 IP6_SERVICE *IpSb;
1822 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
1823
1824 if (This == NULL) {
1825 return EFI_INVALID_PARAMETER;
1826 }
1827
1828 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1829 IpSb = IpInstance->Service;
1830
1831 if (IpSb->LinkLocalDadFail) {
1832 return EFI_DEVICE_ERROR;
1833 }
1834
1835 if (IpInstance->State == IP6_STATE_UNCONFIGED) {
1836 return EFI_NOT_STARTED;
1837 }
1838
1839 Mnp = IpInstance->Service->Mnp;
1840
1841 //
1842 // Don't lock the Poll function to enable the deliver of
1843 // the packet polled up.
1844 //
1845 return Mnp->Poll (Mnp);
1846
1847 }
1848