]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip6Dxe/Ip6Impl.c
365495a5e448d680e10344965cf3b4a716afd72a
[mirror_edk2.git] / NetworkPkg / Ip6Dxe / Ip6Impl.c
1 /** @file
2 Implementation of EFI_IP6_PROTOCOL protocol interfaces.
3
4 Copyright (c) 2009 - 2012, 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 //
709 // Update the variable data.
710 //
711 Ip6SetVariableData (IpInstance->Service);
712
713 Exit:
714 gBS->RestoreTPL (OldTpl);
715 return Status;
716 }
717
718 /**
719 Joins and leaves multicast groups.
720
721 The Groups() function is used to join and leave multicast group sessions. Joining a group will
722 enable reception of matching multicast packets. Leaving a group will disable reception of matching
723 multicast packets. Source-Specific Multicast isn't required to be supported.
724
725 If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
726
727 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
728 @param[in] JoinFlag Set to TRUE to join the multicast group session, and FALSE to leave.
729 @param[in] GroupAddress Pointer to the IPv6 multicast address.
730 This is an optional parameter that may be NULL.
731
732 @retval EFI_SUCCESS The operation completed successfully.
733 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
734 - This is NULL.
735 - JoinFlag is TRUE and GroupAddress is NULL.
736 - GroupAddress is not NULL and *GroupAddress is
737 not a multicast IPv6 address.
738 - GroupAddress is not NULL and *GroupAddress is in the
739 range of SSM destination address.
740 @retval EFI_NOT_STARTED This instance has not been started.
741 @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
742 @retval EFI_UNSUPPORTED This EFI IPv6 Protocol implementation does not support multicast groups.
743 @retval EFI_ALREADY_STARTED The group address is already in the group table (when
744 JoinFlag is TRUE).
745 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
746 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
747
748 **/
749 EFI_STATUS
750 EFIAPI
751 EfiIp6Groups (
752 IN EFI_IP6_PROTOCOL *This,
753 IN BOOLEAN JoinFlag,
754 IN EFI_IPv6_ADDRESS *GroupAddress OPTIONAL
755 )
756 {
757 EFI_TPL OldTpl;
758 EFI_STATUS Status;
759 IP6_PROTOCOL *IpInstance;
760 IP6_SERVICE *IpSb;
761
762 if ((This == NULL) || (JoinFlag && GroupAddress == NULL)) {
763 return EFI_INVALID_PARAMETER;
764 }
765
766 if (GroupAddress != NULL && !IP6_IS_MULTICAST (GroupAddress)) {
767 return EFI_INVALID_PARAMETER;
768 }
769
770 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
771 IpSb = IpInstance->Service;
772
773 if (IpSb->LinkLocalDadFail) {
774 return EFI_DEVICE_ERROR;
775 }
776
777 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
778
779 if (IpInstance->State != IP6_STATE_CONFIGED) {
780 Status = EFI_NOT_STARTED;
781 goto ON_EXIT;
782 }
783
784 Status = Ip6Groups (IpInstance, JoinFlag, GroupAddress);
785
786 ON_EXIT:
787 gBS->RestoreTPL (OldTpl);
788 return Status;
789 }
790
791 /**
792 Adds and deletes routing table entries.
793
794 The Routes() function adds a route to, or deletes a route from, the routing table.
795
796 Routes are determined by comparing the leftmost PrefixLength bits of Destination with
797 the destination IPv6 address arithmetically. The gateway address must be on the same subnet as the
798 configured station address.
799
800 The default route is added with Destination and PrefixLegth both set to all zeros. The
801 default route matches all destination IPv6 addresses that do not match any other routes.
802
803 All EFI IPv6 Protocol instances share a routing table.
804
805 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
806 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
807 FALSE to add this route to the routing table. Destination,
808 PrefixLength and Gateway are used as the key to each
809 route entry.
810 @param[in] Destination The address prefix of the subnet that needs to be routed.
811 This is an optional parameter that may be NULL.
812 @param[in] PrefixLength The prefix length of Destination. Ignored if Destination
813 is NULL.
814 @param[in] GatewayAddress The unicast gateway IPv6 address for this route.
815 This is an optional parameter that may be NULL.
816
817 @retval EFI_SUCCESS The operation completed successfully.
818 @retval EFI_NOT_STARTED The driver instance has not been started.
819 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
820 - This is NULL.
821 - When DeleteRoute is TRUE, both Destination and
822 GatewayAddress are NULL.
823 - When DeleteRoute is FALSE, either Destination or
824 GatewayAddress is NULL.
825 - *GatewayAddress is not a valid unicast IPv6 address.
826 - *GatewayAddress is one of the local configured IPv6
827 addresses.
828 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
829 @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
830 @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
831 DeleteRoute is FALSE).
832
833 **/
834 EFI_STATUS
835 EFIAPI
836 EfiIp6Routes (
837 IN EFI_IP6_PROTOCOL *This,
838 IN BOOLEAN DeleteRoute,
839 IN EFI_IPv6_ADDRESS *Destination OPTIONAL,
840 IN UINT8 PrefixLength,
841 IN EFI_IPv6_ADDRESS *GatewayAddress OPTIONAL
842 )
843 {
844 IP6_PROTOCOL *IpInstance;
845 EFI_STATUS Status;
846 EFI_TPL OldTpl;
847 IP6_SERVICE *IpSb;
848
849 if ((This == NULL) || (PrefixLength >= IP6_PREFIX_NUM)) {
850 return EFI_INVALID_PARAMETER;
851 }
852
853 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
854 IpSb = IpInstance->Service;
855
856 if (IpSb->LinkLocalDadFail) {
857 return EFI_DEVICE_ERROR;
858 }
859
860 if (IpInstance->State != IP6_STATE_CONFIGED) {
861 return EFI_NOT_STARTED;
862 }
863
864 if (DeleteRoute && (Destination == NULL) && (GatewayAddress == NULL)) {
865 return EFI_INVALID_PARAMETER;
866 }
867
868 if (!DeleteRoute && (Destination == NULL || GatewayAddress == NULL)) {
869 return EFI_INVALID_PARAMETER;
870 }
871
872 if (GatewayAddress != NULL) {
873 if (!Ip6IsValidAddress (IpSb, GatewayAddress, FALSE)) {
874 return EFI_INVALID_PARAMETER;
875 }
876
877 if (!NetIp6IsUnspecifiedAddr (GatewayAddress) &&
878 !NetIp6IsNetEqual (GatewayAddress, &IpInstance->ConfigData.StationAddress, PrefixLength)
879 ) {
880 return EFI_INVALID_PARAMETER;
881 }
882 }
883
884 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
885
886 //
887 // Update the route table
888 //
889 if (DeleteRoute) {
890 Status = Ip6DelRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress);
891 } else {
892 Status = Ip6AddRoute (IpSb->RouteTable, Destination, PrefixLength, GatewayAddress);
893 }
894
895 gBS->RestoreTPL (OldTpl);
896 return Status;
897 }
898
899 /**
900 Add or delete Neighbor cache entries.
901
902 The Neighbors() function is used to add, update, or delete an entry from neighbor cache.
903 IPv6 neighbor cache entries are typically inserted and updated by the network protocol driver as
904 network traffic is processed. Most neighbor cache entries will timeout and be deleted if the network
905 traffic stops. Neighbor cache entries that were inserted by Neighbors() may be static (will not
906 timeout) or dynamic (will timeout).
907
908 The implementation should follow the neighbor cache timeout mechanism which is defined in
909 RFC4861. The default neighbor cache timeout value should be tuned for the expected network
910 environment
911
912 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
913 @param[in] DeleteFlag Set to TRUE to delete the specified cache entry, set to FALSE to
914 add (or update, if it already exists and Override is TRUE) the
915 specified cache entry. TargetIp6Address is used as the key
916 to find the requested cache entry.
917 @param[in] TargetIp6Address Pointer to the Target IPv6 address.
918 @param[in] TargetLinkAddress Pointer to the link-layer address of the target. Ignored if NULL.
919 @param[in] Timeout Time in 100-ns units that this entry will remain in the neighbor
920 cache, it will be deleted after Timeout. A value of zero means that
921 the entry is permanent. A non-zero value means that the entry is
922 dynamic.
923 @param[in] Override If TRUE, the cached link-layer address of the matching entry will
924 be overridden and updated; if FALSE, EFI_ACCESS_DENIED
925 will be returned if a corresponding cache entry already existed.
926
927 @retval EFI_SUCCESS The data has been queued for transmission.
928 @retval EFI_NOT_STARTED This instance has not been started.
929 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
930 - This is NULL.
931 - TargetIpAddress is NULL.
932 - *TargetLinkAddress is invalid when not NULL.
933 - *TargetIpAddress is not a valid unicast IPv6 address.
934 - *TargetIpAddress is one of the local configured IPv6
935 addresses.
936 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the neighbor cache.
937 @retval EFI_NOT_FOUND This entry is not in the neighbor cache (when DeleteFlag is
938 TRUE or when DeleteFlag is FALSE while
939 TargetLinkAddress is NULL.).
940 @retval EFI_ACCESS_DENIED The to-be-added entry is already defined in the neighbor cache,
941 and that entry is tagged as un-overridden (when Override
942 is FALSE).
943
944 **/
945 EFI_STATUS
946 EFIAPI
947 EfiIp6Neighbors (
948 IN EFI_IP6_PROTOCOL *This,
949 IN BOOLEAN DeleteFlag,
950 IN EFI_IPv6_ADDRESS *TargetIp6Address,
951 IN EFI_MAC_ADDRESS *TargetLinkAddress OPTIONAL,
952 IN UINT32 Timeout,
953 IN BOOLEAN Override
954 )
955 {
956 EFI_TPL OldTpl;
957 EFI_STATUS Status;
958 IP6_PROTOCOL *IpInstance;
959 IP6_SERVICE *IpSb;
960
961 if (This == NULL || TargetIp6Address == NULL) {
962 return EFI_INVALID_PARAMETER;
963 }
964
965 if (NetIp6IsUnspecifiedAddr (TargetIp6Address)) {
966 return EFI_INVALID_PARAMETER;
967 }
968
969 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
970 IpSb = IpInstance->Service;
971
972 if (IpSb->LinkLocalDadFail) {
973 return EFI_DEVICE_ERROR;
974 }
975
976 if (!Ip6IsValidAddress (IpSb, TargetIp6Address, FALSE)) {
977 return EFI_INVALID_PARAMETER;
978 }
979
980 if (TargetLinkAddress != NULL) {
981 if (!Ip6IsValidLinkAddress (IpSb, TargetLinkAddress)) {
982 return EFI_INVALID_PARAMETER;
983 }
984 }
985
986 if (Ip6IsOneOfSetAddress (IpSb, TargetIp6Address, NULL, NULL)) {
987 return EFI_INVALID_PARAMETER;
988 }
989
990 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
991 if (IpInstance->State != IP6_STATE_CONFIGED) {
992 Status = EFI_NOT_STARTED;
993 goto Exit;
994 }
995
996 if (DeleteFlag) {
997 Status = Ip6DelNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override);
998 } else {
999 Status = Ip6AddNeighbor (IpInstance->Service, TargetIp6Address, TargetLinkAddress, Timeout, Override);
1000 }
1001
1002 Exit:
1003 gBS->RestoreTPL (OldTpl);
1004 return Status;
1005 }
1006
1007 /**
1008 Check whether the user's token or event has already
1009 been enqueue on IP6's list.
1010
1011 @param[in] Map The container of either user's transmit or receive
1012 token.
1013 @param[in] Item Current item to check against.
1014 @param[in] Context The Token to check againist.
1015
1016 @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP
1017 @retval EFI_SUCCESS The current item isn't the same token/event as the
1018 context.
1019
1020 **/
1021 EFI_STATUS
1022 EFIAPI
1023 Ip6TokenExist (
1024 IN NET_MAP *Map,
1025 IN NET_MAP_ITEM *Item,
1026 IN VOID *Context
1027 )
1028 {
1029 EFI_IP6_COMPLETION_TOKEN *Token;
1030 EFI_IP6_COMPLETION_TOKEN *TokenInItem;
1031
1032 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1033 TokenInItem = (EFI_IP6_COMPLETION_TOKEN *) Item->Key;
1034
1035 if (Token == TokenInItem || Token->Event == TokenInItem->Event) {
1036 return EFI_ACCESS_DENIED;
1037 }
1038
1039 return EFI_SUCCESS;
1040 }
1041
1042 /**
1043 Validate the user's token against the current station address.
1044
1045 @param[in] Token User's token to validate.
1046
1047 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1048 @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.
1049 @retval EFI_SUCCESS The token is OK.
1050
1051 **/
1052 EFI_STATUS
1053 Ip6TxTokenValid (
1054 IN EFI_IP6_COMPLETION_TOKEN *Token
1055 )
1056 {
1057 EFI_IP6_TRANSMIT_DATA *TxData;
1058 UINT32 Index;
1059 UINT32 DataLength;
1060
1061 if (Token == NULL || Token->Event == NULL) {
1062 return EFI_INVALID_PARAMETER;
1063 }
1064
1065 TxData = Token->Packet.TxData;
1066
1067 if (TxData == NULL || (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs == NULL)) {
1068 return EFI_INVALID_PARAMETER;
1069 }
1070
1071 if (TxData->FragmentCount == 0 || TxData->DataLength == 0) {
1072 return EFI_INVALID_PARAMETER;
1073 }
1074
1075 for (DataLength = 0, Index = 0; Index < TxData->FragmentCount; Index++) {
1076 if (TxData->FragmentTable[Index].FragmentLength == 0 || TxData->FragmentTable[Index].FragmentBuffer == NULL) {
1077 return EFI_INVALID_PARAMETER;
1078 }
1079
1080 DataLength += TxData->FragmentTable[Index].FragmentLength;
1081 }
1082
1083 if (TxData->DataLength != DataLength) {
1084 return EFI_INVALID_PARAMETER;
1085 }
1086
1087 //
1088 // TODO: Token.Packet.TxData.DataLength is too short to transmit.
1089 // return EFI_BUFFER_TOO_SMALL;
1090 //
1091
1092 //
1093 // If Token.Packet.TxData.DataLength is beyond the maximum that which can be
1094 // described through the Fragment Offset field in Fragment header when performing
1095 // fragmentation.
1096 //
1097 if (TxData->DataLength > 64 * 1024) {
1098 return EFI_BAD_BUFFER_SIZE;
1099 }
1100
1101 return EFI_SUCCESS;
1102 }
1103
1104 /**
1105 The callback function for the net buffer which wraps the user's
1106 transmit token. Although this function seems simple, there
1107 are some subtle aspects.
1108 When user requests the IP to transmit a packet by passing it a
1109 token, the token is wrapped in an IP6_TXTOKEN_WRAP and the data
1110 is wrapped in an net buffer. The net buffer's Free function is
1111 set to Ip6FreeTxToken. The Token and token wrap are added to the
1112 IP child's TxToken map. Then the buffer is passed to Ip6Output for
1113 transmission. If an error happened before that, the buffer
1114 is freed, which in turn frees the token wrap. The wrap may
1115 have been added to the TxToken map or not, and the user's event
1116 shouldn't be fired because we are still in the EfiIp6Transmit. If
1117 the buffer has been sent by Ip6Output, it should be removed from
1118 the TxToken map and user's event signaled. The token wrap and buffer
1119 are bound together. Check the comments in Ip6Output for information
1120 about IP fragmentation.
1121
1122 @param[in] Context The token's wrap.
1123
1124 **/
1125 VOID
1126 EFIAPI
1127 Ip6FreeTxToken (
1128 IN VOID *Context
1129 )
1130 {
1131 IP6_TXTOKEN_WRAP *Wrap;
1132 NET_MAP_ITEM *Item;
1133
1134 Wrap = (IP6_TXTOKEN_WRAP *) Context;
1135
1136 //
1137 // Signal IpSecRecycleEvent to inform IPsec free the memory
1138 //
1139 if (Wrap->IpSecRecycleSignal != NULL) {
1140 gBS->SignalEvent (Wrap->IpSecRecycleSignal);
1141 }
1142
1143 //
1144 // Find the token in the instance's map. EfiIp6Transmit put the
1145 // token to the map. If that failed, NetMapFindKey will return NULL.
1146 //
1147 Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);
1148
1149 if (Item != NULL) {
1150 NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);
1151 }
1152
1153 if (Wrap->Sent) {
1154 gBS->SignalEvent (Wrap->Token->Event);
1155
1156 //
1157 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
1158 //
1159 DispatchDpc ();
1160 }
1161
1162 FreePool (Wrap);
1163 }
1164
1165
1166 /**
1167 The callback function to Ip6Output to update the transmit status.
1168
1169 @param[in] Packet The user's transmit packet.
1170 @param[in] IoStatus The result of the transmission.
1171 @param[in] Flag Not used during transmission.
1172 @param[in] Context The token's wrap.
1173
1174 **/
1175 VOID
1176 Ip6OnPacketSent (
1177 IN NET_BUF *Packet,
1178 IN EFI_STATUS IoStatus,
1179 IN UINT32 Flag,
1180 IN VOID *Context
1181 )
1182 {
1183 IP6_TXTOKEN_WRAP *Wrap;
1184
1185 //
1186 // This is the transmission request from upper layer,
1187 // not the IP6 driver itself.
1188 //
1189 Wrap = (IP6_TXTOKEN_WRAP *) Context;
1190 Wrap->Token->Status = IoStatus;
1191
1192 NetbufFree (Wrap->Packet);
1193 }
1194
1195 /**
1196 Places outgoing data packets into the transmit queue.
1197
1198 The Transmit() function places a sending request in the transmit queue of this
1199 EFI IPv6 Protocol instance. Whenever the packet in the token is sent out or some
1200 errors occur, the event in the token will be signaled, and the status is updated.
1201
1202 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1203 @param[in] Token Pointer to the transmit token.
1204
1205 @retval EFI_SUCCESS The data has been queued for transmission.
1206 @retval EFI_NOT_STARTED This instance has not been started.
1207 @retval EFI_NO_MAPPING The IPv6 driver was responsible for choosing
1208 a source address for this transmission,
1209 but no source address was available for use.
1210 @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
1211 - This is NULL.
1212 - Token is NULL.
1213 - Token.Event is NULL.
1214 - Token.Packet.TxData is NULL.
1215 - Token.Packet.ExtHdrsLength is not zero and
1216 Token.Packet.ExtHdrs is NULL.
1217 - Token.Packet.FragmentCount is zero.
1218 - One or more of the Token.Packet.TxData.
1219 FragmentTable[].FragmentLength fields is zero.
1220 - One or more of the Token.Packet.TxData.
1221 FragmentTable[].FragmentBuffer fields is NULL.
1222 - Token.Packet.TxData.DataLength is zero or not
1223 equal to the sum of fragment lengths.
1224 - Token.Packet.TxData.DestinationAddress is non
1225 zero when DestinationAddress is configured as
1226 non-zero when doing Configure() for this
1227 EFI IPv6 protocol instance.
1228 - Token.Packet.TxData.DestinationAddress is
1229 unspecified when DestinationAddress is unspecified
1230 when doing Configure() for this EFI IPv6 protocol
1231 instance.
1232 @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.
1233 Event was already in the transmit queue.
1234 @retval EFI_NOT_READY The completion token could not be queued because
1235 the transmit queue is full.
1236 @retval EFI_NOT_FOUND Not route is found to destination address.
1237 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
1238 @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
1239 short to transmit.
1240 @retval EFI_BAD_BUFFER_SIZE If Token.Packet.TxData.DataLength is beyond the
1241 maximum that which can be described through the
1242 Fragment Offset field in Fragment header when
1243 performing fragmentation.
1244 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1245
1246 **/
1247 EFI_STATUS
1248 EFIAPI
1249 EfiIp6Transmit (
1250 IN EFI_IP6_PROTOCOL *This,
1251 IN EFI_IP6_COMPLETION_TOKEN *Token
1252 )
1253 {
1254 IP6_SERVICE *IpSb;
1255 IP6_PROTOCOL *IpInstance;
1256 EFI_IP6_CONFIG_DATA *Config;
1257 EFI_STATUS Status;
1258 EFI_TPL OldTpl;
1259 EFI_IP6_HEADER Head;
1260 EFI_IP6_TRANSMIT_DATA *TxData;
1261 EFI_IP6_OVERRIDE_DATA *Override;
1262 IP6_TXTOKEN_WRAP *Wrap;
1263 UINT8 *ExtHdrs;
1264
1265 //
1266 // Check input parameters.
1267 //
1268 if (This == NULL) {
1269 return EFI_INVALID_PARAMETER;
1270 }
1271
1272 ExtHdrs = NULL;
1273
1274 Status = Ip6TxTokenValid (Token);
1275 if (EFI_ERROR (Status)) {
1276 return Status;
1277 }
1278
1279 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1280 IpSb = IpInstance->Service;
1281
1282 if (IpSb->LinkLocalDadFail) {
1283 return EFI_DEVICE_ERROR;
1284 }
1285
1286 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1287
1288 if (IpInstance->State != IP6_STATE_CONFIGED) {
1289 Status = EFI_NOT_STARTED;
1290 goto Exit;
1291 }
1292
1293 Config = &IpInstance->ConfigData;
1294
1295 //
1296 // Check whether the token or signal already existed.
1297 //
1298 if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip6TokenExist, Token))) {
1299 Status = EFI_ACCESS_DENIED;
1300 goto Exit;
1301 }
1302
1303 //
1304 // Build the IP header, fill in the information from ConfigData or OverrideData
1305 //
1306 ZeroMem (&Head, sizeof(EFI_IP6_HEADER));
1307 TxData = Token->Packet.TxData;
1308 IP6_COPY_ADDRESS (&Head.SourceAddress, &Config->StationAddress);
1309 IP6_COPY_ADDRESS (&Head.DestinationAddress, &Config->DestinationAddress);
1310
1311 Status = EFI_INVALID_PARAMETER;
1312
1313 if (NetIp6IsUnspecifiedAddr (&TxData->DestinationAddress)) {
1314 if (NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) {
1315 goto Exit;
1316 }
1317
1318 ASSERT (!NetIp6IsUnspecifiedAddr (&Config->StationAddress));
1319
1320 } else {
1321 //
1322 // StationAddress is unspecified only when ConfigData.Dest is unspecified.
1323 // Use TxData.Dest to override the DestinationAddress.
1324 //
1325 if (!NetIp6IsUnspecifiedAddr (&Config->DestinationAddress)) {
1326 goto Exit;
1327 }
1328
1329 if (NetIp6IsUnspecifiedAddr (&Config->StationAddress)) {
1330 Status = Ip6SelectSourceAddress (
1331 IpSb,
1332 &TxData->DestinationAddress,
1333 &Head.SourceAddress
1334 );
1335 if (EFI_ERROR (Status)) {
1336 goto Exit;
1337 }
1338 }
1339
1340 IP6_COPY_ADDRESS (&Head.DestinationAddress, &TxData->DestinationAddress);
1341 }
1342
1343 //
1344 // Fill in Head infos.
1345 //
1346 Head.NextHeader = Config->DefaultProtocol;
1347 if (TxData->ExtHdrsLength != 0) {
1348 Head.NextHeader = TxData->NextHeader;
1349 }
1350
1351 if (TxData->OverrideData != NULL) {
1352 Override = TxData->OverrideData;
1353 Head.NextHeader = Override->Protocol;
1354 Head.HopLimit = Override->HopLimit;
1355 Head.FlowLabelL = HTONS ((UINT16) Override->FlowLabel);
1356 Head.FlowLabelH = (UINT8) ((Override->FlowLabel >> 16) & 0x0F);
1357 } else {
1358 Head.HopLimit = Config->HopLimit;
1359 Head.FlowLabelL = HTONS ((UINT16) Config->FlowLabel);
1360 Head.FlowLabelH = (UINT8) ((Config->FlowLabel >> 16) & 0x0F);
1361 }
1362
1363 Head.PayloadLength = HTONS ((UINT16) (TxData->ExtHdrsLength + TxData->DataLength));
1364
1365 //
1366 // OK, it survives all the validation check. Wrap the token in
1367 // a IP6_TXTOKEN_WRAP and the data in a netbuf
1368 //
1369 Status = EFI_OUT_OF_RESOURCES;
1370 Wrap = AllocateZeroPool (sizeof (IP6_TXTOKEN_WRAP));
1371 if (Wrap == NULL) {
1372 goto Exit;
1373 }
1374
1375 Wrap->IpInstance = IpInstance;
1376 Wrap->Token = Token;
1377 Wrap->Sent = FALSE;
1378 Wrap->Life = IP6_US_TO_SEC (Config->TransmitTimeout);
1379 Wrap->Packet = NetbufFromExt (
1380 (NET_FRAGMENT *) TxData->FragmentTable,
1381 TxData->FragmentCount,
1382 IP6_MAX_HEADLEN,
1383 0,
1384 Ip6FreeTxToken,
1385 Wrap
1386 );
1387
1388 if (Wrap->Packet == NULL) {
1389 FreePool (Wrap);
1390 goto Exit;
1391 }
1392
1393 Token->Status = EFI_NOT_READY;
1394
1395 Status = NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap);
1396 if (EFI_ERROR (Status)) {
1397 //
1398 // NetbufFree will call Ip6FreeTxToken, which in turn will
1399 // free the IP6_TXTOKEN_WRAP. Now, the token wrap hasn't been
1400 // enqueued.
1401 //
1402 NetbufFree (Wrap->Packet);
1403 goto Exit;
1404 }
1405
1406 //
1407 // Allocate a new buffer to store IPv6 extension headers to avoid updating
1408 // the original data in EFI_IP6_COMPLETION_TOKEN.
1409 //
1410 if (TxData->ExtHdrsLength != 0 && TxData->ExtHdrs != NULL) {
1411 ExtHdrs = (UINT8 *) AllocateCopyPool (TxData->ExtHdrsLength, TxData->ExtHdrs);
1412 if (ExtHdrs == NULL) {
1413 Status = EFI_OUT_OF_RESOURCES;
1414 goto Exit;
1415 }
1416 }
1417
1418 //
1419 // Mark the packet sent before output it. Mark it not sent again if the
1420 // returned status is not EFI_SUCCESS;
1421 //
1422 Wrap->Sent = TRUE;
1423
1424 Status = Ip6Output (
1425 IpSb,
1426 NULL,
1427 IpInstance,
1428 Wrap->Packet,
1429 &Head,
1430 ExtHdrs,
1431 TxData->ExtHdrsLength,
1432 Ip6OnPacketSent,
1433 Wrap
1434 );
1435 if (EFI_ERROR (Status)) {
1436 Wrap->Sent = FALSE;
1437 NetbufFree (Wrap->Packet);
1438 }
1439
1440 Exit:
1441 gBS->RestoreTPL (OldTpl);
1442
1443 if (ExtHdrs != NULL) {
1444 FreePool (ExtHdrs);
1445 }
1446
1447 return Status;
1448 }
1449
1450 /**
1451 Places a receiving request into the receiving queue.
1452
1453 The Receive() function places a completion token into the receive packet queue.
1454 This function is always asynchronous.
1455
1456 The Token.Event field in the completion token must be filled in by the caller
1457 and cannot be NULL. When the receive operation completes, the EFI IPv6 Protocol
1458 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
1459 is signaled.
1460
1461 Current Udp implementation creates an IP child for each Udp child.
1462 It initates a asynchronous receive immediately no matter whether
1463 there is no mapping or not. Therefore, disable the returning EFI_NO_MAPPING for now.
1464 To enable it, the following check must be performed:
1465
1466 if (NetIp6IsUnspecifiedAddr (&Config->StationAddress) && IP6_NO_MAPPING (IpInstance)) {
1467 Status = EFI_NO_MAPPING;
1468 goto Exit;
1469 }
1470
1471 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1472 @param[in] Token Pointer to a token that is associated with the receive data descriptor.
1473
1474 @retval EFI_SUCCESS The receive completion token was cached.
1475 @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started.
1476 @retval EFI_NO_MAPPING When IP6 driver responsible for binding source address to this instance,
1477 while no source address is available for use.
1478 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
1479 - This is NULL.
1480 - Token is NULL.
1481 - Token.Event is NULL.
1482 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
1483 resources (usually memory).
1484 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1485 The EFI IPv6 Protocol instance has been reset to startup defaults.
1486 @retval EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
1487 in the receive queue.
1488 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
1489
1490 **/
1491 EFI_STATUS
1492 EFIAPI
1493 EfiIp6Receive (
1494 IN EFI_IP6_PROTOCOL *This,
1495 IN EFI_IP6_COMPLETION_TOKEN *Token
1496 )
1497 {
1498 IP6_PROTOCOL *IpInstance;
1499 EFI_STATUS Status;
1500 EFI_TPL OldTpl;
1501 IP6_SERVICE *IpSb;
1502
1503 if (This == NULL || Token == NULL || Token->Event == NULL) {
1504 return EFI_INVALID_PARAMETER;
1505 }
1506
1507 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1508 IpSb = IpInstance->Service;
1509
1510 if (IpSb->LinkLocalDadFail) {
1511 return EFI_DEVICE_ERROR;
1512 }
1513
1514 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1515
1516 if (IpInstance->State != IP6_STATE_CONFIGED) {
1517 Status = EFI_NOT_STARTED;
1518 goto Exit;
1519 }
1520
1521 //
1522 // Check whether the toke is already on the receive queue.
1523 //
1524 Status = NetMapIterate (&IpInstance->RxTokens, Ip6TokenExist, Token);
1525
1526 if (EFI_ERROR (Status)) {
1527 Status = EFI_ACCESS_DENIED;
1528 goto Exit;
1529 }
1530
1531 //
1532 // Queue the token then check whether there is pending received packet.
1533 //
1534 Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);
1535
1536 if (EFI_ERROR (Status)) {
1537 goto Exit;
1538 }
1539
1540 Status = Ip6InstanceDeliverPacket (IpInstance);
1541
1542 //
1543 // Dispatch the DPC queued by the NotifyFunction of this instane's receive
1544 // event.
1545 //
1546 DispatchDpc ();
1547
1548 Exit:
1549 gBS->RestoreTPL (OldTpl);
1550 return Status;
1551 }
1552
1553
1554 /**
1555 Cancel the transmitted but not recycled packet. If a matching
1556 token is found, it will call Ip6CancelPacket to cancel the
1557 packet. Ip6CancelPacket cancels all the fragments of the
1558 packet. When all the fragments are freed, the IP6_TXTOKEN_WRAP
1559 is deleted from the Map, and user's event is signalled.
1560 Because Ip6CancelPacket and other functions are all called in
1561 line, after Ip6CancelPacket returns, the Item has been freed.
1562
1563 @param[in] Map The IP6 child's transmit queue.
1564 @param[in] Item The current transmitted packet to test.
1565 @param[in] Context The user's token to cancel.
1566
1567 @retval EFI_SUCCESS Continue to check the next Item.
1568 @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
1569
1570 **/
1571 EFI_STATUS
1572 EFIAPI
1573 Ip6CancelTxTokens (
1574 IN NET_MAP *Map,
1575 IN NET_MAP_ITEM *Item,
1576 IN VOID *Context
1577 )
1578 {
1579 EFI_IP6_COMPLETION_TOKEN *Token;
1580 IP6_TXTOKEN_WRAP *Wrap;
1581
1582 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1583
1584 //
1585 // Return EFI_SUCCESS to check the next item in the map if
1586 // this one doesn't match.
1587 //
1588 if ((Token != NULL) && (Token != Item->Key)) {
1589 return EFI_SUCCESS;
1590 }
1591
1592 Wrap = (IP6_TXTOKEN_WRAP *) Item->Value;
1593 ASSERT (Wrap != NULL);
1594
1595 //
1596 // Don't access the Item, Wrap and Token's members after this point.
1597 // Item and wrap has been freed. And we no longer own the Token.
1598 //
1599 Ip6CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
1600
1601 //
1602 // If only one item is to be cancel, return EFI_ABORTED to stop
1603 // iterating the map any more.
1604 //
1605 if (Token != NULL) {
1606 return EFI_ABORTED;
1607 }
1608
1609 return EFI_SUCCESS;
1610 }
1611
1612
1613 /**
1614 Cancel the receive request. This is simple, because
1615 it is only enqueued in our local receive map.
1616
1617 @param[in] Map The IP6 child's receive queue.
1618 @param[in] Item Current receive request to cancel.
1619 @param[in] Context The user's token to cancel.
1620
1621
1622 @retval EFI_SUCCESS Continue to check the next receive request on the
1623 queue.
1624 @retval EFI_ABORTED The user's token (token != NULL) has been
1625 cancelled.
1626
1627 **/
1628 EFI_STATUS
1629 EFIAPI
1630 Ip6CancelRxTokens (
1631 IN NET_MAP *Map,
1632 IN NET_MAP_ITEM *Item,
1633 IN VOID *Context
1634 )
1635 {
1636 EFI_IP6_COMPLETION_TOKEN *Token;
1637 EFI_IP6_COMPLETION_TOKEN *This;
1638
1639 Token = (EFI_IP6_COMPLETION_TOKEN *) Context;
1640 This = Item->Key;
1641
1642 if ((Token != NULL) && (Token != This)) {
1643 return EFI_SUCCESS;
1644 }
1645
1646 NetMapRemoveItem (Map, Item, NULL);
1647
1648 This->Status = EFI_ABORTED;
1649 This->Packet.RxData = NULL;
1650 gBS->SignalEvent (This->Event);
1651
1652 if (Token != NULL) {
1653 return EFI_ABORTED;
1654 }
1655
1656 return EFI_SUCCESS;
1657 }
1658
1659 /**
1660 Cancel the user's receive/transmit request. It is the worker function of
1661 EfiIp6Cancel API.
1662
1663 @param[in] IpInstance The IP6 child.
1664 @param[in] Token The token to cancel. If NULL, all token will be
1665 cancelled.
1666
1667 @retval EFI_SUCCESS The token is cancelled.
1668 @retval EFI_NOT_FOUND The token isn't found on either the
1669 transmit/receive queue.
1670 @retval EFI_DEVICE_ERROR Not all tokens are cancelled when Token is NULL.
1671
1672 **/
1673 EFI_STATUS
1674 Ip6Cancel (
1675 IN IP6_PROTOCOL *IpInstance,
1676 IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL
1677 )
1678 {
1679 EFI_STATUS Status;
1680
1681 //
1682 // First check the transmitted packet. Ip6CancelTxTokens returns
1683 // EFI_ABORTED to mean that the token has been cancelled when
1684 // token != NULL. So, return EFI_SUCCESS for this condition.
1685 //
1686 Status = NetMapIterate (&IpInstance->TxTokens, Ip6CancelTxTokens, Token);
1687 if (EFI_ERROR (Status)) {
1688 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1689 return EFI_SUCCESS;
1690 }
1691
1692 return Status;
1693 }
1694
1695 //
1696 // Check the receive queue. Ip6CancelRxTokens also returns EFI_ABORT
1697 // for Token!=NULL and it is cancelled.
1698 //
1699 Status = NetMapIterate (&IpInstance->RxTokens, Ip6CancelRxTokens, Token);
1700 //
1701 // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
1702 // events.
1703 //
1704 DispatchDpc ();
1705 if (EFI_ERROR (Status)) {
1706 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1707 return EFI_SUCCESS;
1708 }
1709
1710 return Status;
1711 }
1712
1713 //
1714 // OK, if the Token is found when Token != NULL, the NetMapIterate
1715 // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.
1716 //
1717 if (Token != NULL) {
1718 return EFI_NOT_FOUND;
1719 }
1720
1721 //
1722 // If Token == NULL, cancel all the tokens. return error if not
1723 // all of them are cancelled.
1724 //
1725 if (!NetMapIsEmpty (&IpInstance->TxTokens) || !NetMapIsEmpty (&IpInstance->RxTokens)) {
1726
1727 return EFI_DEVICE_ERROR;
1728 }
1729
1730 return EFI_SUCCESS;
1731 }
1732
1733 /**
1734 Abort an asynchronous transmit or receive request.
1735
1736 The Cancel() function is used to abort a pending transmit or receive request.
1737 If the token is in the transmit or receive request queues, after calling this
1738 function, Token->Status will be set to EFI_ABORTED, and then Token->Event will
1739 be signaled. If the token is not in one of the queues, which usually means the
1740 asynchronous operation has completed, this function will not signal the token,
1741 and EFI_NOT_FOUND is returned.
1742
1743 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1744 @param[in] Token Pointer to a token that has been issued by
1745 EFI_IP6_PROTOCOL.Transmit() or
1746 EFI_IP6_PROTOCOL.Receive(). If NULL, all pending
1747 tokens are aborted. Type EFI_IP6_COMPLETION_TOKEN is
1748 defined in EFI_IP6_PROTOCOL.Transmit().
1749
1750 @retval EFI_SUCCESS The asynchronous I/O request was aborted and
1751 Token->Event was signaled. When Token is NULL, all
1752 pending requests were aborted, and their events were signaled.
1753 @retval EFI_INVALID_PARAMETER This is NULL.
1754 @retval EFI_NOT_STARTED This instance has not been started.
1755 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
1756 not found in the transmit or receive queue. It has either completed
1757 or was not issued by Transmit() and Receive().
1758 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1759
1760 **/
1761 EFI_STATUS
1762 EFIAPI
1763 EfiIp6Cancel (
1764 IN EFI_IP6_PROTOCOL *This,
1765 IN EFI_IP6_COMPLETION_TOKEN *Token OPTIONAL
1766 )
1767 {
1768 IP6_PROTOCOL *IpInstance;
1769 IP6_SERVICE *IpSb;
1770 EFI_STATUS Status;
1771 EFI_TPL OldTpl;
1772
1773 if (This == NULL) {
1774 return EFI_INVALID_PARAMETER;
1775 }
1776
1777 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1778 IpSb = IpInstance->Service;
1779
1780 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1781
1782 if (IpInstance->State != IP6_STATE_CONFIGED) {
1783 Status = EFI_NOT_STARTED;
1784 goto Exit;
1785 }
1786
1787 Status = Ip6Cancel (IpInstance, Token);
1788
1789 Exit:
1790 gBS->RestoreTPL (OldTpl);
1791 return Status;
1792 }
1793
1794 /**
1795 Polls for incoming data packets, and processes outgoing data packets.
1796
1797 The Poll() function polls for incoming data packets and processes outgoing data
1798 packets. Network drivers and applications can call the EFI_IP6_PROTOCOL.Poll()
1799 function to increase the rate that data packets are moved between the communications
1800 device and the transmit and receive queues.
1801
1802 In some systems the periodic timer event may not poll the underlying communications
1803 device fast enough to transmit and/or receive all data packets without missing
1804 incoming packets or dropping outgoing packets. Drivers and applications that are
1805 experiencing packet loss should try calling the EFI_IP6_PROTOCOL.Poll() function
1806 more often.
1807
1808 @param[in] This Pointer to the EFI_IP6_PROTOCOL instance.
1809
1810 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1811 @retval EFI_NOT_STARTED This EFI IPv6 Protocol instance has not been started.
1812 @retval EFI_INVALID_PARAMETER This is NULL.
1813 @retval EFI_DEVICE_ERROR An unexpected system error or network error occurred.
1814 @retval EFI_NOT_READY No incoming or outgoing data was processed.
1815 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
1816 Consider increasing the polling rate.
1817
1818 **/
1819 EFI_STATUS
1820 EFIAPI
1821 EfiIp6Poll (
1822 IN EFI_IP6_PROTOCOL *This
1823 )
1824 {
1825 IP6_PROTOCOL *IpInstance;
1826 IP6_SERVICE *IpSb;
1827 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
1828
1829 if (This == NULL) {
1830 return EFI_INVALID_PARAMETER;
1831 }
1832
1833 IpInstance = IP6_INSTANCE_FROM_PROTOCOL (This);
1834 IpSb = IpInstance->Service;
1835
1836 if (IpSb->LinkLocalDadFail) {
1837 return EFI_DEVICE_ERROR;
1838 }
1839
1840 if (IpInstance->State == IP6_STATE_UNCONFIGED) {
1841 return EFI_NOT_STARTED;
1842 }
1843
1844 Mnp = IpInstance->Service->Mnp;
1845
1846 //
1847 // Don't lock the Poll function to enable the deliver of
1848 // the packet polled up.
1849 //
1850 return Mnp->Poll (Mnp);
1851
1852 }
1853