2 VLAN Config Protocol implementation and VLAN packet process routine.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 VLAN_DEVICE_PATH mVlanDevicePathTemplate
= {
14 MESSAGING_DEVICE_PATH
,
17 (UINT8
) (sizeof (VLAN_DEVICE_PATH
)),
18 (UINT8
) ((sizeof (VLAN_DEVICE_PATH
)) >> 8)
24 EFI_VLAN_CONFIG_PROTOCOL mVlanConfigProtocolTemplate
= {
32 Create a child handle for the VLAN ID.
34 @param[in] ImageHandle The driver image handle.
35 @param[in] ControllerHandle Handle of device to bind driver to.
36 @param[in] VlanId The VLAN ID.
37 @param[out] Devicepath Pointer to returned device path for child handle.
39 @return The handle of VLAN child or NULL if failed to create VLAN child.
44 IN EFI_HANDLE ImageHandle
,
45 IN EFI_HANDLE ControllerHandle
,
47 OUT EFI_DEVICE_PATH_PROTOCOL
**Devicepath OPTIONAL
50 EFI_HANDLE ChildHandle
;
51 VLAN_DEVICE_PATH VlanNode
;
52 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
53 EFI_DEVICE_PATH_PROTOCOL
*VlanDevicePath
;
57 // Try to get parent device path
59 Status
= gBS
->OpenProtocol (
61 &gEfiDevicePathProtocolGuid
,
62 (VOID
**) &ParentDevicePath
,
65 EFI_OPEN_PROTOCOL_GET_PROTOCOL
67 if (EFI_ERROR (Status
)) {
72 // Construct device path for child handle: MAC + VLAN
74 CopyMem (&VlanNode
, &mVlanDevicePathTemplate
, sizeof (VLAN_DEVICE_PATH
));
75 VlanNode
.VlanId
= VlanId
;
76 VlanDevicePath
= AppendDevicePathNode (
78 (EFI_DEVICE_PATH_PROTOCOL
*) &VlanNode
80 if (VlanDevicePath
== NULL
) {
85 // Create child VLAN handle by installing DevicePath protocol
88 Status
= gBS
->InstallMultipleProtocolInterfaces (
90 &gEfiDevicePathProtocolGuid
,
94 if (EFI_ERROR (Status
)) {
95 FreePool (VlanDevicePath
);
99 if (Devicepath
!= NULL
) {
100 *Devicepath
= VlanDevicePath
;
107 Remove VLAN tag from a packet.
109 @param[in, out] MnpDeviceData Pointer to the mnp device context data.
110 @param[in, out] Nbuf Pointer to the NET_BUF to remove VLAN tag.
111 @param[out] VlanId Pointer to the returned VLAN ID.
113 @retval TRUE VLAN tag is removed from this packet.
114 @retval FALSE There is no VLAN tag in this packet.
119 IN OUT MNP_DEVICE_DATA
*MnpDeviceData
,
120 IN OUT NET_BUF
*Nbuf
,
125 UINTN ProtocolOffset
;
129 ProtocolOffset
= MnpDeviceData
->Snp
->Mode
->HwAddressSize
* 2;
132 // Get the packet buffer.
134 Packet
= NetbufGetByte (Nbuf
, 0, NULL
);
135 ASSERT (Packet
!= NULL
);
138 // Check whether this is VLAN tagged frame by Ether Type
141 ProtocolType
= NTOHS (*(UINT16
*) (Packet
+ ProtocolOffset
));
142 if (ProtocolType
!= ETHER_TYPE_VLAN
) {
144 // Not a VLAN tagged frame
149 VlanTag
.Uint16
= NTOHS (*(UINT16
*) (Packet
+ ProtocolOffset
+ sizeof (ProtocolType
)));
150 *VlanId
= VlanTag
.Bits
.Vid
;
153 // Move hardware address (DA + SA) 4 bytes right to override VLAN tag
155 CopyMem (Packet
+ NET_VLAN_TAG_LEN
, Packet
, ProtocolOffset
);
158 // Remove VLAN tag from the Nbuf
160 NetbufTrim (Nbuf
, NET_VLAN_TAG_LEN
, NET_BUF_HEAD
);
167 Build the vlan packet to transmit from the TxData passed in.
169 @param MnpServiceData Pointer to the mnp service context data.
170 @param TxData Pointer to the transmit data containing the
171 information to build the packet.
172 @param ProtocolType Pointer to the Ethernet protocol type.
173 @param Packet Pointer to record the address of the packet.
174 @param Length Pointer to a UINT32 variable used to record the
180 IN MNP_SERVICE_DATA
*MnpServiceData
,
181 IN EFI_MANAGED_NETWORK_TRANSMIT_DATA
*TxData
,
182 OUT UINT16
*ProtocolType
,
183 IN OUT UINT8
**Packet
,
184 IN OUT UINT32
*Length
190 MNP_DEVICE_DATA
*MnpDeviceData
;
191 EFI_SIMPLE_NETWORK_MODE
*SnpMode
;
193 MnpDeviceData
= MnpServiceData
->MnpDeviceData
;
194 SnpMode
= MnpDeviceData
->Snp
->Mode
;
196 *ProtocolType
= ETHER_TYPE_VLAN
;
197 *Length
= *Length
+ NET_VLAN_TAG_LEN
;
198 *Packet
= *Packet
- NET_VLAN_TAG_LEN
;
200 Tpid
= (UINT16
*) (*Packet
+ SnpMode
->MediaHeaderSize
- sizeof (*ProtocolType
));
201 VlanTci
= (VLAN_TCI
*) (UINTN
) (Tpid
+ 1);
202 if (TxData
->HeaderLength
!= 0) {
204 // Media header is in packet, move DA+SA 4 bytes left
208 *Packet
+ NET_VLAN_TAG_LEN
,
209 SnpMode
->MediaHeaderSize
- sizeof (*ProtocolType
)
211 *Tpid
= HTONS (ETHER_TYPE_VLAN
);
214 // Media header not in packet, VLAN TCI and original protocol type becomes payload
216 EtherType
= (UINT16
*) (UINTN
) (VlanTci
+ 1);
217 *EtherType
= HTONS (TxData
->ProtocolType
);
220 VlanTci
->Bits
.Vid
= MnpServiceData
->VlanId
;
221 VlanTci
->Bits
.Cfi
= VLAN_TCI_CFI_CANONICAL_MAC
;
222 VlanTci
->Bits
.Priority
= MnpServiceData
->Priority
;
223 VlanTci
->Uint16
= HTONS (VlanTci
->Uint16
);
227 Check VLAN configuration variable and delete the duplicative content if has identical Vlan ID.
229 @param[in] MnpDeviceData Pointer to the MNP device context data.
230 @param[in] Buffer Pointer to the buffer contains the array of VLAN_TCI.
231 @param[in] NumberOfVlan Pointer to number of VLAN.
232 @param[out] NewNumberOfVlan Pointer to number of unique VLAN.
234 @retval EFI_SUCCESS The VLAN variable is successfully checked.
235 @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration.
239 MnpCheckVlanVariable (
240 IN MNP_DEVICE_DATA
*MnpDeviceData
,
242 IN UINTN NumberOfVlan
,
243 OUT UINTN
*NewNumberOfVlan
249 BOOLEAN FoundDuplicateItem
;
253 FoundDuplicateItem
= FALSE
;
254 Status
= EFI_SUCCESS
;
256 for (Index
= 0; Index
< NumberOfVlan
; Index
++) {
257 for (Index2
= Index
+ 1; Index2
< NumberOfVlan
; Index2
++) {
258 if (Buffer
[Index
].Bits
.Vid
== Buffer
[Index2
].Bits
.Vid
) {
259 FoundDuplicateItem
= TRUE
;
264 if (FoundDuplicateItem
) {
265 for (Index2
= Index
+1; Index2
< NumberOfVlan
; Index
++, Index2
++) {
266 CopyMem (Buffer
+ Index
, Buffer
+ Index2
, sizeof (VLAN_TCI
));
269 FoundDuplicateItem
= FALSE
;
272 *NewNumberOfVlan
= NumberOfVlan
- Count
;
274 Status
= MnpSetVlanVariable (MnpDeviceData
, *NewNumberOfVlan
, Buffer
);
281 Get VLAN configuration variable.
283 @param[in] MnpDeviceData Pointer to the MNP device context data.
284 @param[out] NumberOfVlan Pointer to number of VLAN to be returned.
285 @param[out] VlanVariable Pointer to the buffer to return requested
288 @retval EFI_SUCCESS The array of VLAN_TCI was returned in VlanVariable
289 and number of VLAN was returned in NumberOfVlan.
290 @retval EFI_NOT_FOUND VLAN configuration variable not found.
291 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the configuration.
296 IN MNP_DEVICE_DATA
*MnpDeviceData
,
297 OUT UINTN
*NumberOfVlan
,
298 OUT VLAN_TCI
**VlanVariable
304 UINTN NewNumberOfVlan
;
307 // Get VLAN configuration from EFI Variable
311 Status
= gRT
->GetVariable (
312 MnpDeviceData
->MacString
,
313 &gEfiVlanConfigProtocolGuid
,
318 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
319 return EFI_NOT_FOUND
;
323 // Allocate buffer to read the variable
325 Buffer
= AllocateZeroPool (BufferSize
);
326 if (Buffer
== NULL
) {
327 return EFI_OUT_OF_RESOURCES
;
330 Status
= gRT
->GetVariable (
331 MnpDeviceData
->MacString
,
332 &gEfiVlanConfigProtocolGuid
,
337 if (EFI_ERROR (Status
)) {
342 Status
= MnpCheckVlanVariable (MnpDeviceData
, Buffer
, BufferSize
/ sizeof (VLAN_TCI
), &NewNumberOfVlan
);
343 if (!EFI_ERROR (Status
)) {
344 *NumberOfVlan
= NewNumberOfVlan
;
345 *VlanVariable
= Buffer
;
352 Set VLAN configuration variable.
354 @param[in] MnpDeviceData Pointer to the MNP device context data.
355 @param[in] NumberOfVlan Number of VLAN in array VlanVariable.
356 @param[in] VlanVariable Pointer to array of VLAN_TCI.
358 @retval EFI_SUCCESS The VLAN variable is successfully set.
359 @retval EFI_OUT_OF_RESOURCES There is not enough resource to set the configuration.
364 IN MNP_DEVICE_DATA
*MnpDeviceData
,
365 IN UINTN NumberOfVlan
,
366 IN VLAN_TCI
*VlanVariable
369 return gRT
->SetVariable (
370 MnpDeviceData
->MacString
,
371 &gEfiVlanConfigProtocolGuid
,
372 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
373 NumberOfVlan
* sizeof (VLAN_TCI
),
380 Create a VLAN device or modify the configuration parameter of an
381 already-configured VLAN.
383 The Set() function is used to create a new VLAN device or change the VLAN
384 configuration parameters. If the VlanId hasn't been configured in the
385 physical Ethernet device, a new VLAN device will be created. If a VLAN with
386 this VlanId is already configured, then related configuration will be updated
387 as the input parameters.
389 If VlanId is zero, the VLAN device will send and receive untagged frames.
390 Otherwise, the VLAN device will send and receive VLAN-tagged frames containing the VlanId.
391 If VlanId is out of scope of (0-4094), EFI_INVALID_PARAMETER is returned.
392 If Priority is out of the scope of (0-7), then EFI_INVALID_PARAMETER is returned.
393 If there is not enough system memory to perform the registration, then
394 EFI_OUT_OF_RESOURCES is returned.
396 @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL.
397 @param[in] VlanId A unique identifier (1-4094) of the VLAN which is being created
398 or modified, or zero (0).
399 @param[in] Priority 3 bit priority in VLAN header. Priority 0 is default value. If
400 VlanId is zero (0), Priority is ignored.
402 @retval EFI_SUCCESS The VLAN is successfully configured.
403 @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE:
405 - VlanId is an invalid VLAN Identifier.
406 - Priority is invalid.
407 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to perform the registration.
413 IN EFI_VLAN_CONFIG_PROTOCOL
*This
,
419 MNP_DEVICE_DATA
*MnpDeviceData
;
420 MNP_SERVICE_DATA
*MnpServiceData
;
421 VLAN_TCI
*OldVariable
;
422 VLAN_TCI
*NewVariable
;
428 if ((This
== NULL
) || (VlanId
> 4094) || (Priority
> 7)) {
429 return EFI_INVALID_PARAMETER
;
433 MnpDeviceData
= MNP_DEVICE_DATA_FROM_THIS (This
);
434 if (MnpDeviceData
->NumberOfVlan
== 0) {
436 // No existing VLAN, this is the first VLAN to add
439 Entry
= GetFirstNode (&MnpDeviceData
->ServiceList
);
440 MnpServiceData
= MNP_SERVICE_DATA_FROM_LINK (Entry
);
444 // VlanId is not 0, need destroy the default MNP service data
446 Status
= MnpDestroyServiceChild (MnpServiceData
);
447 if (EFI_ERROR (Status
)) {
451 Status
= MnpDestroyServiceData (MnpServiceData
);
452 if (EFI_ERROR (Status
)) {
457 // Create a new MNP service data for this VLAN
459 MnpServiceData
= MnpCreateServiceData (MnpDeviceData
, VlanId
, Priority
);
460 if (MnpServiceData
== NULL
) {
461 return EFI_OUT_OF_RESOURCES
;
466 // Try to find VlanId in existing VLAN list
468 MnpServiceData
= MnpFindServiceData (MnpDeviceData
, VlanId
);
469 if (MnpServiceData
== NULL
) {
471 // VlanId not found, create a new MNP service data
474 MnpServiceData
= MnpCreateServiceData (MnpDeviceData
, VlanId
, Priority
);
475 if (MnpServiceData
== NULL
) {
476 return EFI_OUT_OF_RESOURCES
;
481 MnpServiceData
->VlanId
= VlanId
;
482 MnpServiceData
->Priority
= Priority
;
484 MnpDeviceData
->NumberOfVlan
++;
488 // Update VLAN configuration variable
493 MnpGetVlanVariable (MnpDeviceData
, &NumberOfVlan
, &OldVariable
);
497 // VLAN not exist - add
499 NewVariable
= AllocateZeroPool ((NumberOfVlan
+ 1) * sizeof (VLAN_TCI
));
500 if (NewVariable
== NULL
) {
501 Status
= EFI_OUT_OF_RESOURCES
;
505 if (OldVariable
!= NULL
) {
506 CopyMem (NewVariable
, OldVariable
, NumberOfVlan
* sizeof (VLAN_TCI
));
509 Index
= NumberOfVlan
++;
512 // VLAN already exist - update
514 for (Index
= 0; Index
< NumberOfVlan
; Index
++) {
515 if (OldVariable
[Index
].Bits
.Vid
== VlanId
) {
519 ASSERT (Index
< NumberOfVlan
);
521 NewVariable
= OldVariable
;
525 NewVariable
[Index
].Bits
.Vid
= VlanId
;
526 NewVariable
[Index
].Bits
.Priority
= Priority
;
528 Status
= MnpSetVlanVariable (MnpDeviceData
, NumberOfVlan
, NewVariable
);
529 FreePool (NewVariable
);
532 if (OldVariable
!= NULL
) {
533 FreePool (OldVariable
);
541 Find configuration information for specified VLAN or all configured VLANs.
543 The Find() function is used to find the configuration information for matching
544 VLAN and allocate a buffer into which those entries are copied.
546 @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL.
547 @param[in] VlanId Pointer to VLAN identifier. Set to NULL to find all
549 @param[out] NumberOfVlan The number of VLANs which is found by the specified criteria.
550 @param[out] Entries The buffer which receive the VLAN configuration.
552 @retval EFI_SUCCESS The VLAN is successfully found.
553 @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE:
555 - Specified VlanId is invalid.
556 @retval EFI_NOT_FOUND No matching VLAN is found.
562 IN EFI_VLAN_CONFIG_PROTOCOL
*This
,
563 IN UINT16
*VlanId OPTIONAL
,
564 OUT UINT16
*NumberOfVlan
,
565 OUT EFI_VLAN_FIND_DATA
**Entries
568 MNP_DEVICE_DATA
*MnpDeviceData
;
569 MNP_SERVICE_DATA
*MnpServiceData
;
571 EFI_VLAN_FIND_DATA
*VlanData
;
573 if ((This
== NULL
) || (VlanId
!= NULL
&& *VlanId
> 4094) || (NumberOfVlan
== NULL
) || (Entries
== NULL
)) {
574 return EFI_INVALID_PARAMETER
;
580 MnpDeviceData
= MNP_DEVICE_DATA_FROM_THIS (This
);
581 if (MnpDeviceData
->NumberOfVlan
== 0) {
582 return EFI_NOT_FOUND
;
585 if (VlanId
== NULL
) {
587 // Return all current VLAN configuration
589 *NumberOfVlan
= (UINT16
) MnpDeviceData
->NumberOfVlan
;
590 VlanData
= AllocateZeroPool (*NumberOfVlan
* sizeof (EFI_VLAN_FIND_DATA
));
591 if (VlanData
== NULL
) {
592 return EFI_OUT_OF_RESOURCES
;
596 NET_LIST_FOR_EACH (Entry
, &MnpDeviceData
->ServiceList
) {
597 MnpServiceData
= MNP_SERVICE_DATA_FROM_LINK (Entry
);
599 VlanData
->VlanId
= MnpServiceData
->VlanId
;
600 VlanData
->Priority
= MnpServiceData
->Priority
;
608 // VlanId is specified, try to find it in current VLAN list
610 MnpServiceData
= MnpFindServiceData (MnpDeviceData
, *VlanId
);
611 if (MnpServiceData
== NULL
) {
612 return EFI_NOT_FOUND
;
615 VlanData
= AllocateZeroPool (sizeof (EFI_VLAN_FIND_DATA
));
616 if (VlanData
== NULL
) {
617 return EFI_OUT_OF_RESOURCES
;
619 VlanData
->VlanId
= MnpServiceData
->VlanId
;
620 VlanData
->Priority
= MnpServiceData
->Priority
;
630 Remove the configured VLAN device.
632 The Remove() function is used to remove the specified VLAN device.
633 If the VlanId is out of the scope of (0-4094), EFI_INVALID_PARAMETER is returned.
634 If specified VLAN hasn't been previously configured, EFI_NOT_FOUND is returned.
636 @param[in] This Points to the EFI_VLAN_CONFIG_PROTOCOL.
637 @param[in] VlanId Identifier (0-4094) of the VLAN to be removed.
639 @retval EFI_SUCCESS The VLAN is successfully removed.
640 @retval EFI_INVALID_PARAMETER One or more of following conditions is TRUE:
642 - VlanId is an invalid parameter.
643 @retval EFI_NOT_FOUND The to-be-removed VLAN does not exist.
649 IN EFI_VLAN_CONFIG_PROTOCOL
*This
,
654 MNP_DEVICE_DATA
*MnpDeviceData
;
655 MNP_SERVICE_DATA
*MnpServiceData
;
657 VLAN_TCI
*VlanVariable
;
660 if ((This
== NULL
) || (VlanId
> 4094)) {
661 return EFI_INVALID_PARAMETER
;
664 MnpDeviceData
= MNP_DEVICE_DATA_FROM_THIS (This
);
665 if (MnpDeviceData
->NumberOfVlan
== 0) {
666 return EFI_NOT_FOUND
;
670 // Try to find the VlanId
672 MnpServiceData
= MnpFindServiceData (MnpDeviceData
, VlanId
);
673 if (MnpServiceData
== NULL
) {
674 return EFI_NOT_FOUND
;
677 MnpDeviceData
->NumberOfVlan
--;
679 if ((VlanId
!= 0) || (MnpDeviceData
->NumberOfVlan
!= 0)) {
681 // If VlanId is not 0 or VlanId is 0 and it is not the last VLAN to remove,
682 // destroy its MNP service data
684 Status
= MnpDestroyServiceChild (MnpServiceData
);
685 if (EFI_ERROR (Status
)) {
689 Status
= MnpDestroyServiceData (MnpServiceData
);
690 if (EFI_ERROR (Status
)) {
695 if ((VlanId
!= 0) && (MnpDeviceData
->NumberOfVlan
== 0)) {
697 // This is the last VLAN to be removed, restore the default MNP service data
699 MnpServiceData
= MnpCreateServiceData (MnpDeviceData
, 0, 0);
700 if (MnpServiceData
== NULL
) {
701 return EFI_OUT_OF_RESOURCES
;
706 // Update VLAN configuration variable
709 if (MnpDeviceData
->NumberOfVlan
!= 0) {
710 VlanVariable
= AllocatePool (MnpDeviceData
->NumberOfVlan
* sizeof (VLAN_TCI
));
711 if (VlanVariable
== NULL
) {
712 return EFI_OUT_OF_RESOURCES
;
715 VlanData
= VlanVariable
;
716 NET_LIST_FOR_EACH (Entry
, &MnpDeviceData
->ServiceList
) {
717 MnpServiceData
= MNP_SERVICE_DATA_FROM_LINK (Entry
);
719 VlanData
->Bits
.Vid
= MnpServiceData
->VlanId
;
720 VlanData
->Bits
.Priority
= MnpServiceData
->Priority
;
725 Status
= MnpSetVlanVariable (MnpDeviceData
, MnpDeviceData
->NumberOfVlan
, VlanVariable
);
727 if (VlanVariable
!= NULL
) {
728 FreePool (VlanVariable
);