Changeset 105670 in vbox for trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c
- Timestamp:
- Aug 14, 2024 1:16:30 PM (4 months ago)
- Location:
- trunk/src/VBox/Devices/EFI/FirmwareNew
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/EFI/FirmwareNew
-
Property svn:mergeinfo
changed from (toggle deleted branches)
to (toggle deleted branches)/vendor/edk2/current 103735-103757,103769-103776,129194-159268 /vendor/edk2/current 103735-103757,103769-103776,129194-164365
-
Property svn:mergeinfo
changed from (toggle deleted branches)
-
trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Dhcp6Dxe/Dhcp6Utility.c
r99404 r105670 10 10 11 11 #include "Dhcp6Impl.h" 12 13 // 14 // Verifies the packet cursor is within the packet 15 // otherwise it is invalid 16 // 17 #define IS_INVALID_PACKET_CURSOR(PacketCursor, Packet) \ 18 (((*PacketCursor) < (Packet)->Dhcp6.Option) || \ 19 ((*PacketCursor) >= (Packet)->Dhcp6.Option + ((Packet)->Size - sizeof(EFI_DHCP6_HEADER))) \ 20 ) \ 21 12 22 13 23 /** … … 578 588 579 589 /** 580 Append the option to Buf, and move Buf to the end. 581 582 @param[in, out] Buf The pointer to the buffer. 583 @param[in] OptType The option type. 584 @param[in] OptLen The length of option contents. 585 @param[in] Data The pointer to the option content. 586 587 @return Buf The position to append the next option. 588 589 **/ 590 UINT8 * 590 Append the option to Buf, update the length of packet, and move Buf to the end. 591 592 @param[in, out] Packet A pointer to the packet, on success Packet->Length 593 will be updated. 594 @param[in, out] PacketCursor The pointer in the packet, on success PacketCursor 595 will be moved to the end of the option. 596 @param[in] OptType The option type. 597 @param[in] OptLen The length of option contents. 598 @param[in] Data The pointer to the option content. 599 600 @retval EFI_INVALID_PARAMETER An argument provided to the function was invalid 601 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to append the option. 602 @retval EFI_SUCCESS The option is appended successfully. 603 604 **/ 605 EFI_STATUS 591 606 Dhcp6AppendOption ( 592 IN OUT UINT8 *Buf, 593 IN UINT16 OptType, 594 IN UINT16 OptLen, 595 IN UINT8 *Data 596 ) 597 { 607 IN OUT EFI_DHCP6_PACKET *Packet, 608 IN OUT UINT8 **PacketCursor, 609 IN UINT16 OptType, 610 IN UINT16 OptLen, 611 IN UINT8 *Data 612 ) 613 { 614 UINT32 Length; 615 UINT32 BytesNeeded; 616 598 617 // 599 618 // The format of Dhcp6 option: … … 608 627 // 609 628 610 ASSERT (OptLen != 0); 611 612 WriteUnaligned16 ((UINT16 *)Buf, OptType); 613 Buf += 2; 614 WriteUnaligned16 ((UINT16 *)Buf, OptLen); 615 Buf += 2; 616 CopyMem (Buf, Data, NTOHS (OptLen)); 617 Buf += NTOHS (OptLen); 618 619 return Buf; 629 // 630 // Verify the arguments are valid 631 // 632 if (Packet == NULL) { 633 return EFI_INVALID_PARAMETER; 634 } 635 636 if ((PacketCursor == NULL) || (*PacketCursor == NULL)) { 637 return EFI_INVALID_PARAMETER; 638 } 639 640 if (Data == NULL) { 641 return EFI_INVALID_PARAMETER; 642 } 643 644 if (OptLen == 0) { 645 return EFI_INVALID_PARAMETER; 646 } 647 648 // 649 // Verify the PacketCursor is within the packet 650 // 651 if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) { 652 return EFI_INVALID_PARAMETER; 653 } 654 655 // 656 // Calculate the bytes needed for the option 657 // 658 BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN + NTOHS (OptLen); 659 660 // 661 // Space remaining in the packet 662 // 663 Length = Packet->Size - Packet->Length; 664 if (Length < BytesNeeded) { 665 return EFI_BUFFER_TOO_SMALL; 666 } 667 668 WriteUnaligned16 ((UINT16 *)*PacketCursor, OptType); 669 *PacketCursor += DHCP6_SIZE_OF_OPT_CODE; 670 WriteUnaligned16 ((UINT16 *)*PacketCursor, OptLen); 671 *PacketCursor += DHCP6_SIZE_OF_OPT_LEN; 672 CopyMem (*PacketCursor, Data, NTOHS (OptLen)); 673 *PacketCursor += NTOHS (OptLen); 674 675 // Update the packet length by the length of the option + 4 bytes 676 Packet->Length += BytesNeeded; 677 678 return EFI_SUCCESS; 620 679 } 621 680 … … 623 682 Append the appointed IA Address option to Buf, and move Buf to the end. 624 683 625 @param[in, out] Buf The pointer to the position to append. 684 @param[in, out] Packet A pointer to the packet, on success Packet->Length 685 will be updated. 686 @param[in, out] PacketCursor The pointer in the packet, on success PacketCursor 687 will be moved to the end of the option. 626 688 @param[in] IaAddr The pointer to the IA Address. 627 689 @param[in] MessageType Message type of DHCP6 package. 628 690 629 @return Buf The position to append the next option. 630 631 **/ 632 UINT8 * 691 @retval EFI_INVALID_PARAMETER An argument provided to the function was invalid 692 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to append the option. 693 @retval EFI_SUCCESS The option is appended successfully. 694 695 **/ 696 EFI_STATUS 633 697 Dhcp6AppendIaAddrOption ( 634 IN OUT UINT8 *Buf, 698 IN OUT EFI_DHCP6_PACKET *Packet, 699 IN OUT UINT8 **PacketCursor, 635 700 IN EFI_DHCP6_IA_ADDRESS *IaAddr, 636 701 IN UINT32 MessageType 637 702 ) 638 703 { 704 UINT32 BytesNeeded; 705 UINT32 Length; 706 639 707 // The format of the IA Address option is: 640 708 // … … 659 727 660 728 // 729 // Verify the arguments are valid 730 // 731 if (Packet == NULL) { 732 return EFI_INVALID_PARAMETER; 733 } 734 735 if ((PacketCursor == NULL) || (*PacketCursor == NULL)) { 736 return EFI_INVALID_PARAMETER; 737 } 738 739 if (IaAddr == NULL) { 740 return EFI_INVALID_PARAMETER; 741 } 742 743 // 744 // Verify the PacketCursor is within the packet 745 // 746 if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) { 747 return EFI_INVALID_PARAMETER; 748 } 749 750 BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN; 751 BytesNeeded += sizeof (EFI_IPv6_ADDRESS); 752 // 753 // Even if the preferred-lifetime is 0, it still needs to store it. 754 // 755 BytesNeeded += sizeof (IaAddr->PreferredLifetime); 756 // 757 // Even if the valid-lifetime is 0, it still needs to store it. 758 // 759 BytesNeeded += sizeof (IaAddr->ValidLifetime); 760 761 // 762 // Space remaining in the packet 763 // 764 Length = Packet->Size - Packet->Length; 765 if (Length < BytesNeeded) { 766 return EFI_BUFFER_TOO_SMALL; 767 } 768 769 // 661 770 // Fill the value of Ia Address option type 662 771 // 663 WriteUnaligned16 ((UINT16 *) Buf, HTONS (Dhcp6OptIaAddr));664 Buf += 2;665 666 WriteUnaligned16 ((UINT16 *) Buf, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS)));667 Buf += 2;668 669 CopyMem ( Buf, &IaAddr->IpAddress, sizeof (EFI_IPv6_ADDRESS));670 Buf+= sizeof (EFI_IPv6_ADDRESS);772 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Dhcp6OptIaAddr)); 773 *PacketCursor += DHCP6_SIZE_OF_OPT_CODE; 774 775 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (sizeof (EFI_DHCP6_IA_ADDRESS))); 776 *PacketCursor += DHCP6_SIZE_OF_OPT_LEN; 777 778 CopyMem (*PacketCursor, &IaAddr->IpAddress, sizeof (EFI_IPv6_ADDRESS)); 779 *PacketCursor += sizeof (EFI_IPv6_ADDRESS); 671 780 672 781 // … … 676 785 // 677 786 if (MessageType != Dhcp6MsgConfirm) { 678 WriteUnaligned32 ((UINT32 *) Buf, HTONL (IaAddr->PreferredLifetime));679 } 680 681 Buf += 4;787 WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (IaAddr->PreferredLifetime)); 788 } 789 790 *PacketCursor += sizeof (IaAddr->PreferredLifetime); 682 791 683 792 if (MessageType != Dhcp6MsgConfirm) { 684 WriteUnaligned32 ((UINT32 *)Buf, HTONL (IaAddr->ValidLifetime)); 685 } 686 687 Buf += 4; 688 689 return Buf; 793 WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (IaAddr->ValidLifetime)); 794 } 795 796 *PacketCursor += sizeof (IaAddr->ValidLifetime); 797 798 // 799 // Update the packet length 800 // 801 Packet->Length += BytesNeeded; 802 803 return EFI_SUCCESS; 690 804 } 691 805 … … 693 807 Append the appointed Ia option to Buf, and move Buf to the end. 694 808 695 @param[in, out] Buf The pointer to the position to append. 809 @param[in, out] Packet A pointer to the packet, on success Packet->Length 810 will be updated. 811 @param[in, out] PacketCursor The pointer in the packet, on success PacketCursor 812 will be moved to the end of the option. 696 813 @param[in] Ia The pointer to the Ia. 697 814 @param[in] T1 The time of T1. … … 699 816 @param[in] MessageType Message type of DHCP6 package. 700 817 701 @return Buf The position to append the next Ia option. 702 703 **/ 704 UINT8 * 818 @retval EFI_INVALID_PARAMETER An argument provided to the function was invalid 819 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to append the option. 820 @retval EFI_SUCCESS The option is appended successfully. 821 822 **/ 823 EFI_STATUS 705 824 Dhcp6AppendIaOption ( 706 IN OUT UINT8 *Buf, 707 IN EFI_DHCP6_IA *Ia, 708 IN UINT32 T1, 709 IN UINT32 T2, 710 IN UINT32 MessageType 711 ) 712 { 713 UINT8 *AddrOpt; 714 UINT16 *Len; 715 UINTN Index; 825 IN OUT EFI_DHCP6_PACKET *Packet, 826 IN OUT UINT8 **PacketCursor, 827 IN EFI_DHCP6_IA *Ia, 828 IN UINT32 T1, 829 IN UINT32 T2, 830 IN UINT32 MessageType 831 ) 832 { 833 UINT8 *AddrOpt; 834 UINT16 *Len; 835 UINTN Index; 836 UINT32 BytesNeeded; 837 UINT32 Length; 838 EFI_STATUS Status; 716 839 717 840 // … … 735 858 736 859 // 860 // Verify the arguments are valid 861 // 862 if (Packet == NULL) { 863 return EFI_INVALID_PARAMETER; 864 } 865 866 if ((PacketCursor == NULL) || (*PacketCursor == NULL)) { 867 return EFI_INVALID_PARAMETER; 868 } 869 870 if (Ia == NULL) { 871 return EFI_INVALID_PARAMETER; 872 } 873 874 // 875 // Verify the PacketCursor is within the packet 876 // 877 if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) { 878 return EFI_INVALID_PARAMETER; 879 } 880 881 BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN; 882 BytesNeeded += sizeof (Ia->Descriptor.IaId); 883 // 884 // + N for the IA_NA-options/IA_TA-options 885 // Dhcp6AppendIaAddrOption will need to check the length for each address 886 // 887 if (Ia->Descriptor.Type == Dhcp6OptIana) { 888 BytesNeeded += sizeof (T1) + sizeof (T2); 889 } 890 891 // 892 // Space remaining in the packet 893 // 894 Length = (UINT16)(Packet->Size - Packet->Length); 895 if (Length < BytesNeeded) { 896 return EFI_BUFFER_TOO_SMALL; 897 } 898 899 // 737 900 // Fill the value of Ia option type 738 901 // 739 WriteUnaligned16 ((UINT16 *) Buf, HTONS (Ia->Descriptor.Type));740 Buf += 2;902 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Ia->Descriptor.Type)); 903 *PacketCursor += DHCP6_SIZE_OF_OPT_CODE; 741 904 742 905 // 743 906 // Fill the len of Ia option later, keep the pointer first 744 907 // 745 Len = (UINT16 *)Buf;746 Buf += 2;908 Len = (UINT16 *)*PacketCursor; 909 *PacketCursor += DHCP6_SIZE_OF_OPT_LEN; 747 910 748 911 // 749 912 // Fill the value of iaid 750 913 // 751 WriteUnaligned32 ((UINT32 *) Buf, HTONL (Ia->Descriptor.IaId));752 Buf += 4;914 WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL (Ia->Descriptor.IaId)); 915 *PacketCursor += sizeof (Ia->Descriptor.IaId); 753 916 754 917 // … … 756 919 // 757 920 if (Ia->Descriptor.Type == Dhcp6OptIana) { 758 WriteUnaligned32 ((UINT32 *)Buf, HTONL ((T1 != 0) ? T1 : 0xffffffff)); 759 Buf += 4; 760 WriteUnaligned32 ((UINT32 *)Buf, HTONL ((T2 != 0) ? T2 : 0xffffffff)); 761 Buf += 4; 762 } 921 WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL ((T1 != 0) ? T1 : 0xffffffff)); 922 *PacketCursor += sizeof (T1); 923 WriteUnaligned32 ((UINT32 *)*PacketCursor, HTONL ((T2 != 0) ? T2 : 0xffffffff)); 924 *PacketCursor += sizeof (T2); 925 } 926 927 // 928 // Update the packet length 929 // 930 Packet->Length += BytesNeeded; 763 931 764 932 // … … 767 935 for (Index = 0; Index < Ia->IaAddressCount; Index++) { 768 936 AddrOpt = (UINT8 *)Ia->IaAddress + Index * sizeof (EFI_DHCP6_IA_ADDRESS); 769 Buf = Dhcp6AppendIaAddrOption (Buf, (EFI_DHCP6_IA_ADDRESS *)AddrOpt, MessageType); 937 Status = Dhcp6AppendIaAddrOption (Packet, PacketCursor, (EFI_DHCP6_IA_ADDRESS *)AddrOpt, MessageType); 938 if (EFI_ERROR (Status)) { 939 return Status; 940 } 770 941 } 771 942 … … 773 944 // Fill the value of Ia option length 774 945 // 775 *Len = HTONS ((UINT16)( Buf- (UINT8 *)Len - 2));776 777 return Buf;946 *Len = HTONS ((UINT16)(*PacketCursor - (UINT8 *)Len - 2)); 947 948 return EFI_SUCCESS; 778 949 } 779 950 … … 781 952 Append the appointed Elapsed time option to Buf, and move Buf to the end. 782 953 783 @param[in, out] Buf The pointer to the position to append. 954 @param[in, out] Packet A pointer to the packet, on success Packet->Length 955 will be updated. 956 @param[in, out] PacketCursor The pointer in the packet, on success PacketCursor 957 will be moved to the end of the option. 784 958 @param[in] Instance The pointer to the Dhcp6 instance. 785 959 @param[out] Elapsed The pointer to the elapsed time value in 786 the generated packet. 787 788 @return Buf The position to append the next Ia option. 789 790 **/ 791 UINT8 * 960 the generated packet. 961 962 @retval EFI_INVALID_PARAMETER An argument provided to the function was invalid 963 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to append the option. 964 @retval EFI_SUCCESS The option is appended successfully. 965 966 **/ 967 EFI_STATUS 792 968 Dhcp6AppendETOption ( 793 IN OUT UINT8 *Buf, 794 IN DHCP6_INSTANCE *Instance, 795 OUT UINT16 **Elapsed 796 ) 797 { 969 IN OUT EFI_DHCP6_PACKET *Packet, 970 IN OUT UINT8 **PacketCursor, 971 IN DHCP6_INSTANCE *Instance, 972 OUT UINT16 **Elapsed 973 ) 974 { 975 UINT32 BytesNeeded; 976 UINT32 Length; 977 798 978 // 799 979 // The format of elapsed time option: … … 808 988 809 989 // 990 // Verify the arguments are valid 991 // 992 if (Packet == NULL) { 993 return EFI_INVALID_PARAMETER; 994 } 995 996 if ((PacketCursor == NULL) || (*PacketCursor == NULL)) { 997 return EFI_INVALID_PARAMETER; 998 } 999 1000 if (Instance == NULL) { 1001 return EFI_INVALID_PARAMETER; 1002 } 1003 1004 if ((Elapsed == NULL)) { 1005 return EFI_INVALID_PARAMETER; 1006 } 1007 1008 // 1009 // Verify the PacketCursor is within the packet 1010 // 1011 if (IS_INVALID_PACKET_CURSOR (PacketCursor, Packet)) { 1012 return EFI_INVALID_PARAMETER; 1013 } 1014 1015 BytesNeeded = DHCP6_SIZE_OF_COMBINED_CODE_AND_LEN; 1016 // 1017 // + 2 for elapsed-time 1018 // 1019 BytesNeeded += sizeof (UINT16); 1020 // 1021 // Space remaining in the packet 1022 // 1023 Length = Packet->Size - Packet->Length; 1024 if (Length < BytesNeeded) { 1025 return EFI_BUFFER_TOO_SMALL; 1026 } 1027 1028 // 810 1029 // Fill the value of elapsed-time option type. 811 1030 // 812 WriteUnaligned16 ((UINT16 *) Buf, HTONS (Dhcp6OptElapsedTime));813 Buf += 2;1031 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (Dhcp6OptElapsedTime)); 1032 *PacketCursor += DHCP6_SIZE_OF_OPT_CODE; 814 1033 815 1034 // 816 1035 // Fill the len of elapsed-time option, which is fixed. 817 1036 // 818 WriteUnaligned16 ((UINT16 *) Buf, HTONS (2));819 Buf += 2;1037 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (2)); 1038 *PacketCursor += DHCP6_SIZE_OF_OPT_LEN; 820 1039 821 1040 // … … 823 1042 // filled in later just before the packet is transmitted. 824 1043 // 825 WriteUnaligned16 ((UINT16 *)Buf, HTONS (0)); 826 *Elapsed = (UINT16 *)Buf; 827 Buf += 2; 828 829 return Buf; 1044 WriteUnaligned16 ((UINT16 *)*PacketCursor, HTONS (0)); 1045 *Elapsed = (UINT16 *)*PacketCursor; 1046 *PacketCursor += sizeof (UINT16); 1047 1048 Packet->Length += BytesNeeded; 1049 1050 return EFI_SUCCESS; 830 1051 } 831 1052
Note:
See TracChangeset
for help on using the changeset viewer.