2 The implementation of IPsec.
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
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.
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.
16 #include "IpSecImpl.h"
17 #include "IkeService.h"
18 #include "IpSecDebug.h"
19 #include "IpSecCryptIo.h"
20 #include "IpSecConfigImpl.h"
23 Check if the specified Address is the Valid Address Range.
25 This function checks if the bytes after prefixed length are all Zero in this
26 Address. This Address is supposed to point to a range address. That means it
27 should gives the correct prefixed address and the bytes outside the prefixed are
30 @param[in] IpVersion The IP version.
31 @param[in] Address Points to EFI_IP_ADDRESS to be checked.
32 @param[in] PrefixLength The PrefixeLength of this address.
34 @retval TRUE The address is a vaild address range.
35 @retval FALSE The address is not a vaild address range.
39 IpSecValidAddressRange (
41 IN EFI_IP_ADDRESS
*Address
,
50 EFI_IP_ADDRESS ZeroAddr
;
52 if (PrefixLength
== 0) {
56 AddrLen
= (UINT8
) ((IpVersion
== IP_VERSION_4
) ? 32 : 128);
58 if (AddrLen
<= PrefixLength
) {
62 Div
= (UINT8
) (PrefixLength
/ 8);
63 Mod
= (UINT8
) (PrefixLength
% 8);
64 Addr
= (UINT8
*) Address
;
65 ZeroMem (&ZeroAddr
, sizeof (EFI_IP_ADDRESS
));
68 // Check whether the mod part of host scope is zero or not.
71 Mask
= (UINT8
) (0xFF << (8 - Mod
));
73 if ((Addr
[Div
] | Mask
) != Mask
) {
80 // Check whether the div part of host scope is zero or not.
85 sizeof (EFI_IP_ADDRESS
) - Div
94 Extrct the Address Range from a Address.
96 This function keep the prefix address and zero other part address.
98 @param[in] Address Point to a specified address.
99 @param[in] PrefixLength The prefix length.
100 @param[out] Range Contain the return Address Range.
104 IpSecExtractAddressRange (
105 IN EFI_IP_ADDRESS
*Address
,
106 IN UINT8 PrefixLength
,
107 OUT EFI_IP_ADDRESS
*Range
115 if (PrefixLength
== 0) {
119 Div
= (UINT8
) (PrefixLength
/ 8);
120 Mod
= (UINT8
) (PrefixLength
% 8);
121 Addr
= (UINT8
*) Range
;
123 CopyMem (Range
, Address
, sizeof (EFI_IP_ADDRESS
));
126 // Zero the mod part of host scope.
129 Mask
= (UINT8
) (0xFF << (8 - Mod
));
130 Addr
[Div
] = (UINT8
) (Addr
[Div
] & Mask
);
134 // Zero the div part of host scope.
136 ZeroMem (&Addr
[Div
], sizeof (EFI_IP_ADDRESS
) - Div
);
141 Checks if the IP Address in the address range of AddressInfos specified.
143 @param[in] IpVersion The IP version.
144 @param[in] IpAddr Point to EFI_IP_ADDRESS to be check.
145 @param[in] AddressInfo A list of EFI_IP_ADDRESS_INFO that is used to check
146 the IP Address is matched.
147 @param[in] AddressCount The total numbers of the AddressInfo.
149 @retval TRUE If the Specified IP Address is in the range of the AddressInfos specified.
150 @retval FALSE If the Specified IP Address is not in the range of the AddressInfos specified.
154 IpSecMatchIpAddress (
156 IN EFI_IP_ADDRESS
*IpAddr
,
157 IN EFI_IP_ADDRESS_INFO
*AddressInfo
,
158 IN UINT32 AddressCount
161 EFI_IP_ADDRESS Range
;
167 for (Index
= 0; Index
< AddressCount
; Index
++) {
169 // Check whether the target address is in the address range
170 // if it's a valid range of address.
172 if (IpSecValidAddressRange (
174 &AddressInfo
[Index
].Address
,
175 AddressInfo
[Index
].PrefixLength
178 // Get the range of the target address belongs to.
180 ZeroMem (&Range
, sizeof (EFI_IP_ADDRESS
));
181 IpSecExtractAddressRange (
183 AddressInfo
[Index
].PrefixLength
,
189 &AddressInfo
[Index
].Address
,
190 sizeof (EFI_IP_ADDRESS
)
193 // The target address is in the address range.
202 &AddressInfo
[Index
].Address
,
203 sizeof (EFI_IP_ADDRESS
)
206 // The target address is exact same as the address.
216 Check if the specified Protocol and Prot is supported by the specified SPD Entry.
218 This function is the subfunction of IPsecLookUpSpdEntry() that is used to
219 check if the sent/received IKE packet has the related SPD entry support.
221 @param[in] Protocol The Protocol to be checked.
222 @param[in] IpPayload Point to IP Payload to be check.
223 @param[in] SpdProtocol The Protocol supported by SPD.
224 @param[in] SpdLocalPort The Local Port in SPD.
225 @param[in] SpdRemotePort The Remote Port in SPD.
226 @param[in] IsOutbound Flag to indicate the is for IKE Packet sending or recieving.
228 @retval TRUE The Protocol and Port are supported by the SPD Entry.
229 @retval FALSE The Protocol and Port are not supported by the SPD Entry.
233 IpSecMatchNextLayerProtocol (
236 IN UINT16 SpdProtocol
,
237 IN UINT16 SpdLocalPort
,
238 IN UINT16 SpdRemotePort
,
239 IN BOOLEAN IsOutbound
244 if (SpdProtocol
== EFI_IPSEC_ANY_PROTOCOL
) {
250 if (SpdProtocol
== Protocol
) {
252 case EFI_IP_PROTO_UDP
:
253 case EFI_IP_PROTO_TCP
:
255 // For udp and tcp, (0, 0) means no need to check local and remote
256 // port. The payload is passed from upper level, which means it should
257 // be in network order.
259 IsMatch
= (BOOLEAN
) (SpdLocalPort
== 0 && SpdRemotePort
== 0);
260 IsMatch
= (BOOLEAN
) (IsMatch
||
263 NTOHS (((EFI_UDP_HEADER
*) IpPayload
)->SrcPort
) == SpdLocalPort
&&
264 NTOHS (((EFI_UDP_HEADER
*) IpPayload
)->DstPort
) == SpdRemotePort
268 IsMatch
= (BOOLEAN
) (IsMatch
||
271 NTOHS (((EFI_UDP_HEADER
*) IpPayload
)->DstPort
) == SpdLocalPort
&&
272 NTOHS (((EFI_UDP_HEADER
*) IpPayload
)->SrcPort
) == SpdRemotePort
277 case EFI_IP_PROTO_ICMP
:
279 // For icmpv4, type code is replaced with local port and remote port,
280 // and (0, 0) means no need to check.
282 IsMatch
= (BOOLEAN
) (SpdLocalPort
== 0 && SpdRemotePort
== 0);
283 IsMatch
= (BOOLEAN
) (IsMatch
||
284 (BOOLEAN
) (((IP4_ICMP_HEAD
*) IpPayload
)->Type
== SpdLocalPort
&&
285 ((IP4_ICMP_HEAD
*) IpPayload
)->Code
== SpdRemotePort
292 // For icmpv6, type code is replaced with local port and remote port,
293 // and (0, 0) means no need to check.
295 IsMatch
= (BOOLEAN
) (SpdLocalPort
== 0 && SpdRemotePort
== 0);
297 IsMatch
= (BOOLEAN
) (IsMatch
||
298 (BOOLEAN
) (((IP6_ICMP_HEAD
*) IpPayload
)->Type
== SpdLocalPort
&&
299 ((IP6_ICMP_HEAD
*) IpPayload
)->Code
== SpdRemotePort
314 Find the SAD through a specified SPD's SAD list.
316 @param[in] SadList SAD list related to a specified SPD entry.
317 @param[in] DestAddress The destination address used to find the SAD entry.
318 @param[in] IpVersion The IP version. Ip4 or Ip6.
320 @return The pointer to a certain SAD entry.
324 IpSecLookupSadBySpd (
325 IN LIST_ENTRY
*SadList
,
326 IN EFI_IP_ADDRESS
*DestAddress
,
331 IPSEC_SAD_ENTRY
*SadEntry
;
333 NET_LIST_FOR_EACH (Entry
, SadList
) {
335 SadEntry
= IPSEC_SAD_ENTRY_FROM_SPD (Entry
);
337 // Find the right SAD entry which contains the appointed dest address.
339 if (IpSecMatchIpAddress (
342 SadEntry
->Data
->SpdSelector
->RemoteAddress
,
343 SadEntry
->Data
->SpdSelector
->RemoteAddressCount
353 Find the SAD through whole SAD list.
355 @param[in] Spi The SPI used to search the SAD entry.
356 @param[in] DestAddress The destination used to search the SAD entry.
357 @param[in] IpVersion The IP version. Ip4 or Ip6.
359 @return the pointer to a certain SAD entry.
363 IpSecLookupSadBySpi (
365 IN EFI_IP_ADDRESS
*DestAddress
,
371 IPSEC_SAD_ENTRY
*SadEntry
;
373 SadList
= &mConfigData
[IPsecConfigDataTypeSad
];
375 NET_LIST_FOR_EACH (Entry
, SadList
) {
377 SadEntry
= IPSEC_SAD_ENTRY_FROM_LIST (Entry
);
380 // Find the right SAD entry which contain the appointed spi and dest addr.
382 if (SadEntry
->Id
->Spi
== Spi
) {
383 if (SadEntry
->Data
->Mode
== EfiIPsecTunnel
) {
386 &SadEntry
->Data
->TunnelDestAddress
,
387 sizeof (EFI_IP_ADDRESS
)
392 if (SadEntry
->Data
->SpdSelector
!= NULL
&&
393 IpSecMatchIpAddress (
396 SadEntry
->Data
->SpdSelector
->RemoteAddress
,
397 SadEntry
->Data
->SpdSelector
->RemoteAddressCount
409 Look up if there is existing SAD entry for specified IP packet sending.
411 This function is called by the IPsecProcess when there is some IP packet needed to
412 send out. This function checks if there is an existing SAD entry that can be serviced
413 to this IP packet sending. If no existing SAD entry could be used, this
414 function will invoke an IPsec Key Exchange Negotiation.
416 @param[in] Private Points to private data.
417 @param[in] NicHandle Points to a NIC handle.
418 @param[in] IpVersion The version of IP.
419 @param[in] IpHead The IP Header of packet to be sent out.
420 @param[in] IpPayload The IP Payload to be sent out.
421 @param[in] OldLastHead The Last protocol of the IP packet.
422 @param[in] SpdEntry Points to a related SPD entry.
423 @param[out] SadEntry Contains the Point of a related SAD entry.
425 @retval EFI_DEVICE_ERROR One of following conditions is TRUE:
426 - If don't find related UDP service.
427 - Sequence Number is used up.
428 - Extension Sequence Number is used up.
429 @retval EFI_NOT_READY No existing SAD entry could be used.
430 @retval EFI_SUCCESS Find the related SAD entry.
434 IpSecLookupSadEntry (
435 IN IPSEC_PRIVATE_DATA
*Private
,
436 IN EFI_HANDLE NicHandle
,
440 IN UINT8 OldLastHead
,
441 IN IPSEC_SPD_ENTRY
*SpdEntry
,
442 OUT IPSEC_SAD_ENTRY
**SadEntry
445 IKE_UDP_SERVICE
*UdpService
;
446 IPSEC_SAD_ENTRY
*Entry
;
447 IPSEC_SAD_DATA
*Data
;
448 EFI_IP_ADDRESS DestIp
;
452 UdpService
= IkeLookupUdp (Private
, NicHandle
, IpVersion
);
454 if (UdpService
== NULL
) {
455 return EFI_DEVICE_ERROR
;
458 // Parse the destination address from ip header.
460 ZeroMem (&DestIp
, sizeof (EFI_IP_ADDRESS
));
461 if (IpVersion
== IP_VERSION_4
) {
464 &((IP4_HEAD
*) IpHead
)->Dst
,
470 &((EFI_IP6_HEADER
*) IpHead
)->DestinationAddress
,
471 sizeof (EFI_IP_ADDRESS
)
476 // Find the SAD entry in the spd.sas list according to the dest address.
478 Entry
= IpSecLookupSadBySpd (&SpdEntry
->Data
->Sas
, &DestIp
, IpVersion
);
482 if (OldLastHead
!= IP6_ICMP
||
483 (OldLastHead
== IP6_ICMP
&& *IpPayload
== ICMP_V6_ECHO_REQUEST
)
486 // Start ike negotiation process except the request packet of ping.
488 if (SpdEntry
->Data
->ProcessingPolicy
->Mode
== EfiIPsecTunnel
) {
492 &SpdEntry
->Data
->ProcessingPolicy
->TunnelOption
->RemoteTunnelAddress
504 return EFI_NOT_READY
;
509 if (!Data
->ManualSet
) {
510 if (Data
->ESNEnabled
) {
512 // Validate the 64bit sn number if 64bit sn enabled.
514 if ((UINT64
) (Data
->SequenceNumber
+ 1) == 0) {
516 // TODO: Re-negotiate SA
518 return EFI_DEVICE_ERROR
;
522 // Validate the 32bit sn number if 64bit sn disabled.
524 SeqNum32
= (UINT32
) Data
->SequenceNumber
;
525 if ((UINT32
) (SeqNum32
+ 1) == 0) {
527 // TODO: Re-negotiate SA
529 return EFI_DEVICE_ERROR
;
540 Find a PAD entry according to a remote IP address.
542 @param[in] IpVersion The version of IP.
543 @param[in] IpAddr Points to remote IP address.
545 @return the pointer of related PAD entry.
549 IpSecLookupPadEntry (
551 IN EFI_IP_ADDRESS
*IpAddr
556 EFI_IP_ADDRESS_INFO
*IpAddrInfo
;
557 IPSEC_PAD_ENTRY
*PadEntry
;
559 PadList
= &mConfigData
[IPsecConfigDataTypePad
];
561 for (Entry
= PadList
->ForwardLink
; Entry
!= PadList
; Entry
= Entry
->ForwardLink
) {
563 PadEntry
= IPSEC_PAD_ENTRY_FROM_LIST (Entry
);
564 IpAddrInfo
= &PadEntry
->Id
->Id
.IpAddress
;
566 // Find the right pad entry which contain the appointed dest addr.
568 if (IpSecMatchIpAddress (IpVersion
, IpAddr
, IpAddrInfo
, 1)) {
577 Check if the specified IP packet can be serviced by this SPD entry.
579 @param[in] SpdEntry Point to SPD entry.
580 @param[in] IpVersion Version of IP.
581 @param[in] IpHead Point to IP header.
582 @param[in] IpPayload Point to IP payload.
583 @param[in] Protocol The Last protocol of IP packet.
584 @param[in] IsOutbound Traffic direction.
585 @param[out] Action The support action of SPD entry.
587 @retval EFI_SUCCESS Find the related SPD.
588 @retval EFI_NOT_FOUND Not find the related SPD entry;
592 IpSecLookupSpdEntry (
593 IN IPSEC_SPD_ENTRY
*SpdEntry
,
598 IN BOOLEAN IsOutbound
,
599 OUT EFI_IPSEC_ACTION
*Action
602 EFI_IPSEC_SPD_SELECTOR
*SpdSel
;
605 EFI_IP_ADDRESS SrcAddr
;
606 EFI_IP_ADDRESS DstAddr
;
609 ASSERT (SpdEntry
!= NULL
);
610 SpdSel
= SpdEntry
->Selector
;
611 Ip4
= (IP4_HEAD
*) IpHead
;
612 Ip6
= (EFI_IP6_HEADER
*) IpHead
;
614 ZeroMem (&SrcAddr
, sizeof (EFI_IP_ADDRESS
));
615 ZeroMem (&DstAddr
, sizeof (EFI_IP_ADDRESS
));
618 // Parse the source and destination address from ip header.
620 if (IpVersion
== IP_VERSION_4
) {
621 CopyMem (&SrcAddr
, &Ip4
->Src
, sizeof (IP4_ADDR
));
622 CopyMem (&DstAddr
, &Ip4
->Dst
, sizeof (IP4_ADDR
));
624 CopyMem (&SrcAddr
, &Ip6
->SourceAddress
, sizeof (EFI_IPv6_ADDRESS
));
625 CopyMem (&DstAddr
, &Ip6
->DestinationAddress
, sizeof (EFI_IPv6_ADDRESS
));
628 // Check the local and remote addresses for outbound traffic
630 SpdMatch
= (BOOLEAN
)(IsOutbound
&&
631 IpSecMatchIpAddress (
634 SpdSel
->LocalAddress
,
635 SpdSel
->LocalAddressCount
637 IpSecMatchIpAddress (
640 SpdSel
->RemoteAddress
,
641 SpdSel
->RemoteAddressCount
646 // Check the local and remote addresses for inbound traffic
648 SpdMatch
= (BOOLEAN
) (SpdMatch
||
650 IpSecMatchIpAddress (
653 SpdSel
->LocalAddress
,
654 SpdSel
->LocalAddressCount
656 IpSecMatchIpAddress (
659 SpdSel
->RemoteAddress
,
660 SpdSel
->RemoteAddressCount
665 // Check the next layer protocol and local and remote ports.
667 SpdMatch
= (BOOLEAN
) (SpdMatch
&&
668 IpSecMatchNextLayerProtocol (
671 SpdSel
->NextLayerProtocol
,
680 // Find the right SPD entry if match the 5 key elements.
682 *Action
= SpdEntry
->Data
->Action
;
686 return EFI_NOT_FOUND
;
690 The call back function of NetbufFromExt.
692 @param[in] Arg The argument passed from the caller.
697 IpSecOnRecyclePacket (
704 This is a Notification function. It is called when the related IP6_TXTOKEN_WRAP
707 @param[in] Event The related event.
708 @param[in] Context The data passed by the caller.
713 IpSecRecycleCallback (
718 IPSEC_RECYCLE_CONTEXT
*RecycleContext
;
720 RecycleContext
= (IPSEC_RECYCLE_CONTEXT
*) Context
;
722 if (RecycleContext
->FragmentTable
!= NULL
) {
723 FreePool (RecycleContext
->FragmentTable
);
726 if (RecycleContext
->PayloadBuffer
!= NULL
) {
727 FreePool (RecycleContext
->PayloadBuffer
);
730 FreePool (RecycleContext
);
731 gBS
->CloseEvent (Event
);
736 Calculate the extension hader of IP. The return length only doesn't contain
737 the fixed IP header length.
739 @param[in] IpHead Points to an IP head to be calculated.
740 @param[in] LastHead Points to the last header of the IP header.
742 @return The length of the extension header.
746 IpSecGetPlainExtHeadSize (
753 Size
= (UINT16
) (LastHead
- (UINT8
*) IpHead
);
755 if (Size
> sizeof (EFI_IP6_HEADER
)) {
757 // * (LastHead+1) point the last header's length but not include the first
758 // 8 octers, so this formluation add 8 at the end.
760 Size
= (UINT16
) (Size
- sizeof (EFI_IP6_HEADER
) + *(LastHead
+ 1) + 8);
769 Verify if the Authentication payload is correct.
771 @param[in] EspBuffer Points to the ESP wrapped buffer.
772 @param[in] EspSize The size of the ESP wrapped buffer.
773 @param[in] SadEntry The related SAD entry to store the authentication
775 @param[in] IcvSize The length of ICV.
777 @retval EFI_SUCCESS The authentication data is correct.
778 @retval EFI_ACCESS_DENIED The authentication data is not correct.
782 IpSecEspAuthVerifyPayload (
785 IN IPSEC_SAD_ENTRY
*SadEntry
,
792 HASH_DATA_FRAGMENT HashFragment
[1];
795 // Calculate the size of authentication payload.
797 AuthSize
= EspSize
- IcvSize
;
800 // Calculate the icv buffer and size of the payload.
802 HashFragment
[0].Data
= EspBuffer
;
803 HashFragment
[0].DataSize
= AuthSize
;
805 Status
= IpSecCryptoIoHmac (
806 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthAlgoId
,
807 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthKey
,
808 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthKeyLength
,
814 if (EFI_ERROR (Status
)) {
819 // Compare the calculated icv and the appended original icv.
821 if (CompareMem (EspBuffer
+ AuthSize
, IcvBuffer
, IcvSize
) == 0) {
825 DEBUG ((DEBUG_ERROR
, "Error auth verify payload\n"));
826 return EFI_ACCESS_DENIED
;
830 Search the related SAD entry by the input .
832 @param[in] IpHead The pointer to IP header.
833 @param[in] IpVersion The version of IP (IP4 or IP6).
834 @param[in] Spi The SPI used to search the related SAD entry.
837 @retval NULL Not find the related SAD entry.
838 @retval IPSEC_SAD_ENTRY Return the related SAD entry.
842 IpSecFoundSadFromInboundPacket (
848 EFI_IP_ADDRESS DestIp
;
851 // Parse destination address from ip header.
853 ZeroMem (&DestIp
, sizeof (EFI_IP_ADDRESS
));
854 if (IpVersion
== IP_VERSION_4
) {
857 &((IP4_HEAD
*) IpHead
)->Dst
,
863 &((EFI_IP6_HEADER
*) IpHead
)->DestinationAddress
,
864 sizeof (EFI_IPv6_ADDRESS
)
869 // Lookup SAD entry according to the spi and dest address.
871 return IpSecLookupSadBySpi (Spi
, &DestIp
, IpVersion
);
875 Validate the IP6 extension header format for both the packets we received
876 and that we will transmit.
878 @param[in] NextHeader The next header field in IPv6 basic header.
879 @param[in] ExtHdrs The first bye of the option.
880 @param[in] ExtHdrsLen The length of the whole option.
881 @param[out] LastHeader The pointer of NextHeader of the last extension
882 header processed by IP6.
883 @param[out] RealExtsLen The length of extension headers processed by IP6 layer.
884 This is an optional parameter that may be NULL.
886 @retval TRUE The option is properly formated.
887 @retval FALSE The option is malformated.
891 IpSecIsIp6ExtsValid (
892 IN UINT8
*NextHeader
,
894 IN UINT32 ExtHdrsLen
,
895 OUT UINT8
**LastHeader
,
896 OUT UINT32
*RealExtsLen OPTIONAL
907 if (RealExtsLen
!= NULL
) {
911 *LastHeader
= NextHeader
;
913 if (ExtHdrs
== NULL
&& ExtHdrsLen
== 0) {
917 if ((ExtHdrs
== NULL
&& ExtHdrsLen
!= 0) || (ExtHdrs
!= NULL
&& ExtHdrsLen
== 0)) {
927 while (Pointer
<= ExtHdrsLen
) {
929 switch (*NextHeader
) {
940 case IP6_DESTINATION
:
941 if (*NextHeader
== IP6_DESTINATION
) {
949 NextHeader
= ExtHdrs
+ Pointer
;
952 Option
= ExtHdrs
+ Pointer
;
953 OptionLen
= (UINT8
) ((*Option
+ 1) * 8 - 2);
957 Pointer
= Pointer
+ OptionLen
;
965 // RFC2402, AH header should after fragment header.
971 NextHeader
= ExtHdrs
+ Pointer
;
972 Pointer
= Pointer
+ 8;
980 Option
= ExtHdrs
+ Pointer
;
984 // RFC2402, Payload length is specified in 32-bit words, minus "2".
986 OptionLen
= (UINT8
) ((*Option
+ 2) * 4);
987 Pointer
= Pointer
+ OptionLen
;
991 *LastHeader
= NextHeader
;
992 if (RealExtsLen
!= NULL
) {
993 *RealExtsLen
= Pointer
;
1000 *LastHeader
= NextHeader
;
1002 if (RealExtsLen
!= NULL
) {
1003 *RealExtsLen
= Pointer
;
1010 The actual entry to process the tunnel header and inner header for tunnel mode
1013 This function is the subfunction of IpSecEspInboundPacket(). It change the destination
1014 Ip address to the station address and recalculate the uplayyer's checksum.
1017 @param[in, out] IpHead Points to the IP header containing the ESP header
1018 to be trimed on input, and without ESP header
1020 @param[in] IpPayload The decrypted Ip payload. It start from the inner
1022 @param[in] IpVersion The version of IP.
1023 @param[in] SadData Pointer of the relevant SAD.
1024 @param[in, out] LastHead The Last Header in IP header on return.
1028 IpSecTunnelInboundPacket (
1029 IN OUT UINT8
*IpHead
,
1030 IN UINT8
*IpPayload
,
1032 IN IPSEC_SAD_DATA
*SadData
,
1033 IN OUT UINT8
*LastHead
1036 EFI_UDP_HEADER
*UdpHeader
;
1037 TCP_HEAD
*TcpHeader
;
1039 UINT16 PseudoChecksum
;
1040 UINT16 PacketChecksum
;
1042 IP6_ICMP_HEAD
*Icmp6Head
;
1046 if (IpVersion
== IP_VERSION_4
) {
1048 // Zero OutIP header use this to indicate the input packet is under
1049 // IPsec Tunnel protected.
1056 &((IP4_HEAD
*)IpPayload
)->Dst
,
1057 &SadData
->TunnelDestAddress
.v4
,
1058 sizeof (EFI_IPv4_ADDRESS
)
1062 // Recalculate IpHeader Checksum
1064 if (((IP4_HEAD
*)(IpPayload
))->Checksum
!= 0 ) {
1065 ((IP4_HEAD
*)(IpPayload
))->Checksum
= 0;
1066 ((IP4_HEAD
*)(IpPayload
))->Checksum
= (UINT16
) (~NetblockChecksum (
1068 ((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2
1075 // Recalcualte PseudoChecksum
1077 switch (((IP4_HEAD
*)IpPayload
)->Protocol
) {
1078 case EFI_IP_PROTO_UDP
:
1079 UdpHeader
= (EFI_UDP_HEADER
*)((UINT8
*)IpPayload
+ (((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2));
1080 Checksum
= & UdpHeader
->Checksum
;
1084 case EFI_IP_PROTO_TCP
:
1085 TcpHeader
= (TCP_HEAD
*) ((UINT8
*)IpPayload
+ (((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2));
1086 Checksum
= &TcpHeader
->Checksum
;
1093 PacketChecksum
= NetblockChecksum (
1094 (UINT8
*)IpPayload
+ (((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2),
1095 NTOHS (((IP4_HEAD
*)IpPayload
)->TotalLen
) - (((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2)
1097 PseudoChecksum
= NetPseudoHeadChecksum (
1098 ((IP4_HEAD
*)IpPayload
)->Src
,
1099 ((IP4_HEAD
*)IpPayload
)->Dst
,
1100 ((IP4_HEAD
*)IpPayload
)->Protocol
,
1104 if (Checksum
!= NULL
) {
1105 *Checksum
= NetAddChecksum (PacketChecksum
, PseudoChecksum
);
1106 *Checksum
= (UINT16
) ~(NetAddChecksum (*Checksum
, HTONS((UINT16
)(NTOHS (((IP4_HEAD
*)IpPayload
)->TotalLen
) - (((IP4_HEAD
*)IpPayload
)->HeadLen
<< 2)))));
1110 // Zero OutIP header use this to indicate the input packet is under
1111 // IPsec Tunnel protected.
1115 sizeof (EFI_IP6_HEADER
)
1118 &((EFI_IP6_HEADER
*)IpPayload
)->DestinationAddress
,
1119 &SadData
->TunnelDestAddress
.v6
,
1120 sizeof (EFI_IPv6_ADDRESS
)
1124 // Get the Extension Header and Header length.
1126 IpSecIsIp6ExtsValid (
1127 &((EFI_IP6_HEADER
*)IpPayload
)->NextHeader
,
1128 IpPayload
+ sizeof (EFI_IP6_HEADER
),
1129 ((EFI_IP6_HEADER
*)IpPayload
)->PayloadLength
,
1135 // Recalcualte PseudoChecksum
1137 switch (*LastHead
) {
1138 case EFI_IP_PROTO_UDP
:
1139 UdpHeader
= (EFI_UDP_HEADER
*)((UINT8
*)IpPayload
+ sizeof (EFI_IP6_HEADER
) + OptionLen
);
1140 Checksum
= &UdpHeader
->Checksum
;
1144 case EFI_IP_PROTO_TCP
:
1145 TcpHeader
= (TCP_HEAD
*)(IpPayload
+ sizeof (EFI_IP6_HEADER
) + OptionLen
);
1146 Checksum
= &TcpHeader
->Checksum
;
1151 Icmp6Head
= (IP6_ICMP_HEAD
*) (IpPayload
+ sizeof (EFI_IP6_HEADER
) + OptionLen
);
1152 Checksum
= &Icmp6Head
->Checksum
;
1156 PacketChecksum
= NetblockChecksum (
1157 IpPayload
+ sizeof (EFI_IP6_HEADER
) + OptionLen
,
1158 NTOHS(((EFI_IP6_HEADER
*)IpPayload
)->PayloadLength
) - OptionLen
1160 PseudoChecksum
= NetIp6PseudoHeadChecksum (
1161 &((EFI_IP6_HEADER
*)IpPayload
)->SourceAddress
,
1162 &((EFI_IP6_HEADER
*)IpPayload
)->DestinationAddress
,
1167 if (Checksum
!= NULL
) {
1168 *Checksum
= NetAddChecksum (PacketChecksum
, PseudoChecksum
);
1169 *Checksum
= (UINT16
) ~(NetAddChecksum (
1171 HTONS ((UINT16
)((NTOHS (((EFI_IP6_HEADER
*)(IpPayload
))->PayloadLength
)) - OptionLen
))
1178 The actual entry to create inner header for tunnel mode inbound traffic.
1180 This function is the subfunction of IpSecEspOutboundPacket(). It create
1181 the sending packet by encrypting its payload and inserting ESP header in the orginal
1182 IP header, then return the IpHeader and IPsec protected Fragmentable.
1184 @param[in, out] IpHead Points to IP header containing the orginal IP header
1185 to be processed on input, and inserted ESP header
1187 @param[in] IpVersion The version of IP.
1188 @param[in] SadData The related SAD data.
1189 @param[in, out] LastHead The Last Header in IP header.
1190 @param[in] OptionsBuffer Pointer to the options buffer.
1191 @param[in] OptionsLength Length of the options buffer.
1192 @param[in, out] FragmentTable Pointer to a list of fragments to be protected by
1193 IPsec on input, and with IPsec protected
1195 @param[in] FragmentCount The number of fragments.
1197 @retval EFI_SUCCESS The operation was successful.
1198 @retval EFI_OUT_OF_RESOURCES The required system resources can't be allocated.
1202 IpSecTunnelOutboundPacket (
1203 IN OUT UINT8
*IpHead
,
1205 IN IPSEC_SAD_DATA
*SadData
,
1206 IN OUT UINT8
*LastHead
,
1207 IN VOID
**OptionsBuffer
,
1208 IN UINT32
*OptionsLength
,
1209 IN OUT EFI_IPSEC_FRAGMENT_DATA
**FragmentTable
,
1210 IN UINT32
*FragmentCount
1215 UINT16 PacketChecksum
;
1217 UINT16 PseudoChecksum
;
1218 IP6_ICMP_HEAD
*IcmpHead
;
1221 if (OptionsLength
== NULL
) {
1225 if (IpVersion
== IP_VERSION_4
) {
1226 InnerHead
= AllocateZeroPool (sizeof (IP4_HEAD
) + *OptionsLength
);
1227 ASSERT (InnerHead
!= NULL
);
1234 InnerHead
+ sizeof (IP4_HEAD
),
1239 InnerHead
= AllocateZeroPool (sizeof (EFI_IP6_HEADER
) + *OptionsLength
);
1240 ASSERT (InnerHead
!= NULL
);
1244 sizeof (EFI_IP6_HEADER
)
1247 InnerHead
+ sizeof (EFI_IP6_HEADER
),
1252 if (OptionsBuffer
!= NULL
) {
1253 if (*OptionsLength
!= 0) {
1255 *OptionsBuffer
= NULL
;
1261 // 2. Reassamlbe Fragment into Packet
1263 Packet
= NetbufFromExt (
1264 (NET_FRAGMENT
*)(*FragmentTable
),
1268 IpSecOnRecyclePacket
,
1271 ASSERT (Packet
!= NULL
);
1273 // 3. Check the Last Header, if it is TCP, UDP or ICMP recalcualate its pesudo
1276 switch (*LastHead
) {
1277 case EFI_IP_PROTO_UDP
:
1278 Packet
->Udp
= (EFI_UDP_HEADER
*) NetbufGetByte (Packet
, 0, 0);
1279 ASSERT (Packet
->Udp
!= NULL
);
1280 Checksum
= &Packet
->Udp
->Checksum
;
1284 case EFI_IP_PROTO_TCP
:
1285 Packet
->Tcp
= (TCP_HEAD
*) NetbufGetByte (Packet
, 0, 0);
1286 ASSERT (Packet
->Tcp
!= NULL
);
1287 Checksum
= &Packet
->Tcp
->Checksum
;
1292 IcmpHead
= (IP6_ICMP_HEAD
*) NetbufGetByte (Packet
, 0, NULL
);
1293 ASSERT (IcmpHead
!= NULL
);
1294 Checksum
= &IcmpHead
->Checksum
;
1302 PacketChecksum
= NetbufChecksum (Packet
);
1304 if (IpVersion
== IP_VERSION_4
) {
1306 // Replace the source address of Inner Header.
1309 &((IP4_HEAD
*)InnerHead
)->Src
,
1310 &SadData
->SpdSelector
->LocalAddress
[0].Address
.v4
,
1311 sizeof (EFI_IPv4_ADDRESS
)
1314 PacketChecksum
= NetbufChecksum (Packet
);
1315 PseudoChecksum
= NetPseudoHeadChecksum (
1316 ((IP4_HEAD
*)InnerHead
)->Src
,
1317 ((IP4_HEAD
*)InnerHead
)->Dst
,
1324 // Replace the source address of Inner Header.
1327 &((EFI_IP6_HEADER
*)InnerHead
)->SourceAddress
,
1328 &(SadData
->SpdSelector
->LocalAddress
[0].Address
.v6
),
1329 sizeof (EFI_IPv6_ADDRESS
)
1331 PacketChecksum
= NetbufChecksum (Packet
);
1332 PseudoChecksum
= NetIp6PseudoHeadChecksum (
1333 &((EFI_IP6_HEADER
*)InnerHead
)->SourceAddress
,
1334 &((EFI_IP6_HEADER
*)InnerHead
)->DestinationAddress
,
1340 if (Checksum
!= NULL
) {
1341 *Checksum
= NetAddChecksum (PacketChecksum
, PseudoChecksum
);
1342 *Checksum
= (UINT16
) ~(NetAddChecksum ((UINT16
)*Checksum
, HTONS ((UINT16
) Packet
->TotalSize
)));
1345 if (Packet
!= NULL
) {
1346 NetbufFree (Packet
);
1352 The actual entry to relative function processes the inbound traffic of ESP header.
1354 This function is the subfunction of IpSecProtectInboundPacket(). It checks the
1355 received packet security property and trim the ESP header and then returns without
1356 an IPsec protected IP Header and FramgmentTable.
1358 @param[in] IpVersion The version of IP.
1359 @param[in, out] IpHead Points to the IP header containing the ESP header
1360 to be trimed on input, and without ESP header
1362 @param[out] LastHead The Last Header in IP header on return.
1363 @param[in, out] OptionsBuffer Pointer to the options buffer.
1364 @param[in, out] OptionsLength Length of the options buffer.
1365 @param[in, out] FragmentTable Pointer to a list of fragments in the form of IPsec
1366 protected on input, and without IPsec protected
1368 @param[in, out] FragmentCount The number of fragments.
1369 @param[out] SpdSelector Pointer to contain the address of SPD selector on return.
1370 @param[out] RecycleEvent The event for recycling of resources.
1372 @retval EFI_SUCCESS The operation was successful.
1373 @retval EFI_ACCESS_DENIED One or more following conditions is TRUE:
1374 - ESP header was not found or mal-format.
1375 - The related SAD entry was not found.
1376 - The related SAD entry does not support the ESP protocol.
1377 @retval EFI_OUT_OF_RESOURCES The required system resource can't be allocated.
1381 IpSecEspInboundPacket (
1383 IN OUT VOID
*IpHead
,
1384 OUT UINT8
*LastHead
,
1385 IN OUT VOID
**OptionsBuffer
,
1386 IN OUT UINT32
*OptionsLength
,
1387 IN OUT EFI_IPSEC_FRAGMENT_DATA
**FragmentTable
,
1388 IN OUT UINT32
*FragmentCount
,
1389 OUT EFI_IPSEC_SPD_SELECTOR
**SpdSelector
,
1390 OUT EFI_EVENT
*RecycleEvent
1399 UINTN PlainPayloadSize
;
1402 UINT8
*ProcessBuffer
;
1403 EFI_ESP_HEADER
*EspHeader
;
1404 EFI_ESP_TAIL
*EspTail
;
1405 EFI_IPSEC_SA_ID
*SaId
;
1406 IPSEC_SAD_DATA
*SadData
;
1407 IPSEC_SAD_ENTRY
*SadEntry
;
1408 IPSEC_RECYCLE_CONTEXT
*RecycleContext
;
1410 UINT16 IpSecHeadSize
;
1413 Status
= EFI_SUCCESS
;
1415 ProcessBuffer
= NULL
;
1416 RecycleContext
= NULL
;
1417 *RecycleEvent
= NULL
;
1418 PlainPayloadSize
= 0;
1422 // Build netbuf from fragment table first.
1424 Payload
= NetbufFromExt (
1425 (NET_FRAGMENT
*) *FragmentTable
,
1428 sizeof (EFI_ESP_HEADER
),
1429 IpSecOnRecyclePacket
,
1432 if (Payload
== NULL
) {
1433 Status
= EFI_OUT_OF_RESOURCES
;
1438 // Get the esp size and esp header from netbuf.
1440 EspSize
= Payload
->TotalSize
;
1441 EspHeader
= (EFI_ESP_HEADER
*) NetbufGetByte (Payload
, 0, NULL
);
1443 if (EspHeader
== NULL
) {
1444 Status
= EFI_ACCESS_DENIED
;
1449 // Parse destination address from ip header and found the related SAD Entry.
1451 SadEntry
= IpSecFoundSadFromInboundPacket (
1454 NTOHL (EspHeader
->Spi
)
1457 if (SadEntry
== NULL
) {
1458 Status
= EFI_ACCESS_DENIED
;
1462 SaId
= SadEntry
->Id
;
1463 SadData
= SadEntry
->Data
;
1466 // Only support esp protocol currently.
1468 if (SaId
->Proto
!= EfiIPsecESP
) {
1469 Status
= EFI_ACCESS_DENIED
;
1473 if (!SadData
->ManualSet
) {
1475 // TODO: Check SA lifetime and sequence number
1480 // Allocate buffer for decryption and authentication.
1482 ProcessBuffer
= AllocateZeroPool (EspSize
);
1483 if (ProcessBuffer
== NULL
) {
1484 Status
= EFI_OUT_OF_RESOURCES
;
1488 NetbufCopy (Payload
, 0, (UINT32
) EspSize
, ProcessBuffer
);
1491 // Get the IcvSize for authentication and BlockSize/IvSize for Decryption.
1493 IcvSize
= IpSecGetIcvLength (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthAlgoId
);
1494 IvSize
= IpSecGetEncryptIvLength (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
);
1495 BlockSize
= IpSecGetEncryptBlockSize (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
);
1498 // Make sure the ESP packet is not mal-formt.
1499 // 1. Check whether the Espsize is larger than ESP header + IvSize + EspTail + IcvSize.
1500 // 2. Check whether the left payload size is multiple of IvSize.
1502 MiscSize
= sizeof (EFI_ESP_HEADER
) + IvSize
+ IcvSize
;
1503 if (EspSize
<= (MiscSize
+ sizeof (EFI_ESP_TAIL
))) {
1504 Status
= EFI_ACCESS_DENIED
;
1507 if ((EspSize
- MiscSize
) % BlockSize
!= 0) {
1508 Status
= EFI_ACCESS_DENIED
;
1513 // Authenticate the ESP packet.
1515 if (SadData
->AlgoInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
1516 Status
= IpSecEspAuthVerifyPayload (
1522 if (EFI_ERROR (Status
)) {
1527 // Decrypt the payload by the SAD entry if it has decrypt key.
1529 if (SadData
->AlgoInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
1530 Status
= IpSecCryptoIoDecrypt (
1531 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
,
1532 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncKey
,
1533 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncKeyLength
<< 3,
1534 ProcessBuffer
+ sizeof (EFI_ESP_HEADER
),
1535 ProcessBuffer
+ sizeof (EFI_ESP_HEADER
) + IvSize
,
1536 EspSize
- sizeof (EFI_ESP_HEADER
) - IvSize
- IcvSize
,
1537 ProcessBuffer
+ sizeof (EFI_ESP_HEADER
) + IvSize
1539 if (EFI_ERROR (Status
)) {
1545 // Parse EspTail and compute the plain payload size.
1547 EspTail
= (EFI_ESP_TAIL
*) (ProcessBuffer
+ EspSize
- IcvSize
- sizeof (EFI_ESP_TAIL
));
1548 PaddingSize
= EspTail
->PaddingLength
;
1549 NextHeader
= EspTail
->NextHeader
;
1551 if (EspSize
<= (MiscSize
+ sizeof (EFI_ESP_TAIL
) + PaddingSize
)) {
1552 Status
= EFI_ACCESS_DENIED
;
1555 PlainPayloadSize
= EspSize
- MiscSize
- sizeof (EFI_ESP_TAIL
) - PaddingSize
;
1558 // TODO: handle anti-replay window
1561 // Decryption and authentication with esp has been done, so it's time to
1562 // reload the new packet, create recycle event and fixup ip header.
1564 RecycleContext
= AllocateZeroPool (sizeof (IPSEC_RECYCLE_CONTEXT
));
1565 if (RecycleContext
== NULL
) {
1566 Status
= EFI_OUT_OF_RESOURCES
;
1570 Status
= gBS
->CreateEvent (
1573 IpSecRecycleCallback
,
1577 if (EFI_ERROR (Status
)) {
1582 // The caller will take responsible to handle the original fragment table
1584 *FragmentTable
= AllocateZeroPool (sizeof (EFI_IPSEC_FRAGMENT_DATA
));
1585 if (*FragmentTable
== NULL
) {
1586 Status
= EFI_OUT_OF_RESOURCES
;
1590 RecycleContext
->PayloadBuffer
= ProcessBuffer
;
1591 RecycleContext
->FragmentTable
= *FragmentTable
;
1594 // If Tunnel, recalculate upper-layyer PesudoCheckSum and trim the out
1596 if (SadData
->Mode
== EfiIPsecTunnel
) {
1597 InnerHead
= ProcessBuffer
+ sizeof (EFI_ESP_HEADER
) + IvSize
;
1598 IpSecTunnelInboundPacket (
1606 if (IpVersion
== IP_VERSION_4
) {
1607 (*FragmentTable
)[0].FragmentBuffer
= InnerHead
;
1608 (*FragmentTable
)[0].FragmentLength
= (UINT32
) PlainPayloadSize
;
1611 (*FragmentTable
)[0].FragmentBuffer
= InnerHead
;
1612 (*FragmentTable
)[0].FragmentLength
= (UINT32
) PlainPayloadSize
;
1615 (*FragmentTable
)[0].FragmentBuffer
= ProcessBuffer
+ sizeof (EFI_ESP_HEADER
) + IvSize
;
1616 (*FragmentTable
)[0].FragmentLength
= (UINT32
) PlainPayloadSize
;
1622 // Update the total length field in ip header since processed by esp.
1624 if (!SadData
->Mode
== EfiIPsecTunnel
) {
1625 if (IpVersion
== IP_VERSION_4
) {
1626 ((IP4_HEAD
*) IpHead
)->TotalLen
= HTONS ((UINT16
) ((((IP4_HEAD
*) IpHead
)->HeadLen
<< 2) + PlainPayloadSize
));
1628 IpSecHeadSize
= IpSecGetPlainExtHeadSize (IpHead
, LastHead
);
1629 ((EFI_IP6_HEADER
*) IpHead
)->PayloadLength
= HTONS ((UINT16
)(IpSecHeadSize
+ PlainPayloadSize
));
1632 // Update the next layer field in ip header since esp header inserted.
1634 *LastHead
= NextHeader
;
1639 // Update the SPD association of the SAD entry.
1641 *SpdSelector
= SadData
->SpdSelector
;
1644 if (Payload
!= NULL
) {
1645 NetbufFree (Payload
);
1648 if (EFI_ERROR (Status
)) {
1649 if (ProcessBuffer
!= NULL
) {
1650 FreePool (ProcessBuffer
);
1653 if (RecycleContext
!= NULL
) {
1654 FreePool (RecycleContext
);
1657 if (*RecycleEvent
!= NULL
) {
1658 gBS
->CloseEvent (*RecycleEvent
);
1666 The actual entry to the relative function processes the output traffic using the ESP protocol.
1668 This function is the subfunction of IpSecProtectOutboundPacket(). It protected
1669 the sending packet by encrypting its payload and inserting ESP header in the orginal
1670 IP header, then return the IpHeader and IPsec protected Fragmentable.
1672 @param[in] IpVersion The version of IP.
1673 @param[in, out] IpHead Points to IP header containing the orginal IP header
1674 to be processed on input, and inserted ESP header
1676 @param[in, out] LastHead The Last Header in IP header.
1677 @param[in, out] OptionsBuffer Pointer to the options buffer.
1678 @param[in, out] OptionsLength Length of the options buffer.
1679 @param[in, out] FragmentTable Pointer to a list of fragments to be protected by
1680 IPsec on input, and with IPsec protected
1682 @param[in, out] FragmentCount The number of fragments.
1683 @param[in] SadEntry The related SAD entry.
1684 @param[out] RecycleEvent The event for recycling of resources.
1686 @retval EFI_SUCCESS The operation was successful.
1687 @retval EFI_OUT_OF_RESOURCES The required system resources can't be allocated.
1691 IpSecEspOutboundPacket (
1693 IN OUT VOID
*IpHead
,
1694 IN OUT UINT8
*LastHead
,
1695 IN OUT VOID
**OptionsBuffer
,
1696 IN OUT UINT32
*OptionsLength
,
1697 IN OUT EFI_IPSEC_FRAGMENT_DATA
**FragmentTable
,
1698 IN OUT UINT32
*FragmentCount
,
1699 IN IPSEC_SAD_ENTRY
*SadEntry
,
1700 OUT EFI_EVENT
*RecycleEvent
1705 EFI_IPSEC_SA_ID
*SaId
;
1706 IPSEC_SAD_DATA
*SadData
;
1707 IPSEC_RECYCLE_CONTEXT
*RecycleContext
;
1708 UINT8
*ProcessBuffer
;
1710 INTN EncryptBlockSize
;// Size of encryption block, 4 bytes aligned and >= 4
1711 UINTN EspSize
; // Total size of esp wrapped ip payload
1712 UINTN IvSize
; // Size of IV, optional, might be 0
1713 UINTN PlainPayloadSize
;// Original IP payload size
1714 UINTN PaddingSize
; // Size of padding
1715 UINTN EncryptSize
; // Size of data to be encrypted, start after IV and
1717 UINTN IcvSize
; // Size of ICV, optional, might be 0
1718 UINT8
*RestOfPayload
; // Start of Payload after IV
1719 UINT8
*Padding
; // Start address of padding
1720 EFI_ESP_HEADER
*EspHeader
; // Start address of ESP frame
1721 EFI_ESP_TAIL
*EspTail
; // Address behind padding
1723 HASH_DATA_FRAGMENT HashFragment
[1];
1725 Status
= EFI_ACCESS_DENIED
;
1726 SaId
= SadEntry
->Id
;
1727 SadData
= SadEntry
->Data
;
1728 ProcessBuffer
= NULL
;
1729 RecycleContext
= NULL
;
1730 *RecycleEvent
= NULL
;
1733 if (!SadData
->ManualSet
&&
1734 SadData
->AlgoInfo
.EspAlgoInfo
.EncKey
== NULL
&&
1735 SadData
->AlgoInfo
.EspAlgoInfo
.AuthKey
== NULL
1738 // Invalid manual SAD entry configuration.
1744 // Create OutHeader according to Inner Header
1746 if (SadData
->Mode
== EfiIPsecTunnel
) {
1747 InnerHead
= IpSecTunnelOutboundPacket (
1758 if (InnerHead
== NULL
) {
1759 return EFI_INVALID_PARAMETER
;
1765 // Calculate enctrypt block size, need iv by default and 4 bytes alignment.
1767 EncryptBlockSize
= 4;
1769 if (SadData
->AlgoInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
1770 EncryptBlockSize
= IpSecGetEncryptBlockSize (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
);
1772 if (EncryptBlockSize
< 0 || (EncryptBlockSize
!= 1 && EncryptBlockSize
% 4 != 0)) {
1778 // Calculate the plain payload size accroding to the fragment table.
1780 PlainPayloadSize
= 0;
1781 for (Index
= 0; Index
< *FragmentCount
; Index
++) {
1782 PlainPayloadSize
+= (*FragmentTable
)[Index
].FragmentLength
;
1786 // Add IPHeader size for Tunnel Mode
1788 if (SadData
->Mode
== EfiIPsecTunnel
) {
1789 if (IpVersion
== IP_VERSION_4
) {
1790 PlainPayloadSize
+= sizeof (IP4_HEAD
);
1792 PlainPayloadSize
+= sizeof (EFI_IP6_HEADER
);
1795 // OPtions should be encryption into it
1797 PlainPayloadSize
+= *OptionsLength
;
1802 // Calculate icv size, optional by default and 4 bytes alignment.
1805 if (SadData
->AlgoInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
1806 IcvSize
= IpSecGetIcvLength (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthAlgoId
);
1807 if (IcvSize
% 4 != 0) {
1813 // Calcuate the total size of esp wrapped ip payload.
1815 IvSize
= IpSecGetEncryptIvLength (SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
);
1816 EncryptSize
= (PlainPayloadSize
+ sizeof (EFI_ESP_TAIL
) + EncryptBlockSize
- 1) / EncryptBlockSize
* EncryptBlockSize
;
1817 PaddingSize
= EncryptSize
- PlainPayloadSize
- sizeof (EFI_ESP_TAIL
);
1818 EspSize
= sizeof (EFI_ESP_HEADER
) + IvSize
+ EncryptSize
+ IcvSize
;
1820 ProcessBuffer
= AllocateZeroPool (EspSize
);
1821 if (ProcessBuffer
== NULL
) {
1822 Status
= EFI_OUT_OF_RESOURCES
;
1827 // Calculate esp header and esp tail including header, payload and padding.
1829 EspHeader
= (EFI_ESP_HEADER
*) ProcessBuffer
;
1830 RestOfPayload
= (UINT8
*) (EspHeader
+ 1) + IvSize
;
1831 Padding
= RestOfPayload
+ PlainPayloadSize
;
1832 EspTail
= (EFI_ESP_TAIL
*) (Padding
+ PaddingSize
);
1835 // Fill the sn and spi fields in esp header.
1837 EspHeader
->SequenceNumber
= HTONL ((UINT32
) SadData
->SequenceNumber
+ 1);
1838 //EspHeader->SequenceNumber = HTONL ((UINT32) SadData->SequenceNumber);
1839 EspHeader
->Spi
= HTONL (SaId
->Spi
);
1842 // Copy the rest of payload (after iv) from the original fragment buffer.
1849 if (SadData
->Mode
== EfiIPsecTunnel
) {
1850 if (IpVersion
== IP_VERSION_4
) {
1852 // HeadLen, Total Length
1854 ((IP4_HEAD
*)InnerHead
)->HeadLen
= (UINT8
) ((sizeof (IP4_HEAD
) + *OptionsLength
) >> 2);
1855 ((IP4_HEAD
*)InnerHead
)->TotalLen
= HTONS ((UINT16
) PlainPayloadSize
);
1856 ((IP4_HEAD
*)InnerHead
)->Checksum
= 0;
1857 ((IP4_HEAD
*)InnerHead
)->Checksum
= (UINT16
) (~NetblockChecksum (
1862 RestOfPayload
+ BytesCopied
,
1864 sizeof (IP4_HEAD
) + *OptionsLength
1866 BytesCopied
+= sizeof (IP4_HEAD
) + *OptionsLength
;
1869 ((EFI_IP6_HEADER
*)InnerHead
)->PayloadLength
= HTONS ((UINT16
) (PlainPayloadSize
- sizeof (EFI_IP6_HEADER
)));
1871 RestOfPayload
+ BytesCopied
,
1873 sizeof (EFI_IP6_HEADER
) + *OptionsLength
1875 BytesCopied
+= sizeof (EFI_IP6_HEADER
) + *OptionsLength
;
1879 for (Index
= 0; Index
< *FragmentCount
; Index
++) {
1881 (RestOfPayload
+ BytesCopied
),
1882 (*FragmentTable
)[Index
].FragmentBuffer
,
1883 (*FragmentTable
)[Index
].FragmentLength
1885 BytesCopied
+= (*FragmentTable
)[Index
].FragmentLength
;
1888 // Fill the padding buffer by natural number sequence.
1890 for (Index
= 0; Index
< PaddingSize
; Index
++) {
1891 Padding
[Index
] = (UINT8
) (Index
+ 1);
1894 // Fill the padding length and next header fields in esp tail.
1896 EspTail
->PaddingLength
= (UINT8
) PaddingSize
;
1897 EspTail
->NextHeader
= *LastHead
;
1900 // Fill the next header for Tunnel mode.
1902 if (SadData
->Mode
== EfiIPsecTunnel
) {
1903 if (IpVersion
== IP_VERSION_4
) {
1904 EspTail
->NextHeader
= 4;
1906 EspTail
->NextHeader
= 41;
1911 // Generate iv at random by crypt library.
1913 Status
= IpSecGenerateIv (
1914 (UINT8
*) (EspHeader
+ 1),
1919 if (EFI_ERROR (Status
)) {
1924 // Encryption the payload (after iv) by the SAD entry if has encrypt key.
1926 if (SadData
->AlgoInfo
.EspAlgoInfo
.EncKey
!= NULL
) {
1927 Status
= IpSecCryptoIoEncrypt (
1928 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncAlgoId
,
1929 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncKey
,
1930 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.EncKeyLength
<< 3,
1931 (UINT8
*)(EspHeader
+ 1),
1937 if (EFI_ERROR (Status
)) {
1943 // Authenticate the esp wrapped buffer by the SAD entry if it has auth key.
1945 if (SadData
->AlgoInfo
.EspAlgoInfo
.AuthKey
!= NULL
) {
1947 HashFragment
[0].Data
= ProcessBuffer
;
1948 HashFragment
[0].DataSize
= EspSize
- IcvSize
;
1949 Status
= IpSecCryptoIoHmac (
1950 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthAlgoId
,
1951 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthKey
,
1952 SadEntry
->Data
->AlgoInfo
.EspAlgoInfo
.AuthKeyLength
,
1955 ProcessBuffer
+ EspSize
- IcvSize
,
1958 if (EFI_ERROR (Status
)) {
1964 // Encryption and authentication with esp has been done, so it's time to
1965 // reload the new packet, create recycle event and fixup ip header.
1967 RecycleContext
= AllocateZeroPool (sizeof (IPSEC_RECYCLE_CONTEXT
));
1968 if (RecycleContext
== NULL
) {
1969 Status
= EFI_OUT_OF_RESOURCES
;
1973 Status
= gBS
->CreateEvent (
1976 IpSecRecycleCallback
,
1980 if (EFI_ERROR (Status
)) {
1984 // Caller take responsible to handle the original fragment table.
1986 *FragmentTable
= AllocateZeroPool (sizeof (EFI_IPSEC_FRAGMENT_DATA
));
1987 if (*FragmentTable
== NULL
) {
1988 Status
= EFI_OUT_OF_RESOURCES
;
1992 RecycleContext
->FragmentTable
= *FragmentTable
;
1993 RecycleContext
->PayloadBuffer
= ProcessBuffer
;
1994 (*FragmentTable
)[0].FragmentBuffer
= ProcessBuffer
;
1995 (*FragmentTable
)[0].FragmentLength
= (UINT32
) EspSize
;
1999 // Update the total length field in ip header since processed by esp.
2001 if (IpVersion
== IP_VERSION_4
) {
2002 ((IP4_HEAD
*) IpHead
)->TotalLen
= HTONS ((UINT16
) ((((IP4_HEAD
*) IpHead
)->HeadLen
<< 2) + EspSize
));
2004 ((EFI_IP6_HEADER
*) IpHead
)->PayloadLength
= (UINT16
) (IpSecGetPlainExtHeadSize (IpHead
, LastHead
) + EspSize
);
2008 // If tunnel mode, it should change the outer Ip header with tunnel source address
2009 // and destination tunnel address.
2011 if (SadData
->Mode
== EfiIPsecTunnel
) {
2012 if (IpVersion
== IP_VERSION_4
) {
2014 &((IP4_HEAD
*) IpHead
)->Src
,
2015 &SadData
->TunnelSourceAddress
.v4
,
2016 sizeof (EFI_IPv4_ADDRESS
)
2019 &((IP4_HEAD
*) IpHead
)->Dst
,
2020 &SadData
->TunnelDestAddress
.v4
,
2021 sizeof (EFI_IPv4_ADDRESS
)
2025 &((EFI_IP6_HEADER
*) IpHead
)->SourceAddress
,
2026 &SadData
->TunnelSourceAddress
.v6
,
2027 sizeof (EFI_IPv6_ADDRESS
)
2030 &((EFI_IP6_HEADER
*) IpHead
)->DestinationAddress
,
2031 &SadData
->TunnelDestAddress
.v6
,
2032 sizeof (EFI_IPv6_ADDRESS
)
2038 // Update the next layer field in ip header since esp header inserted.
2040 *LastHead
= IPSEC_ESP_PROTOCOL
;
2043 // Increase the sn number in SAD entry according to rfc4303.
2045 SadData
->SequenceNumber
++;
2048 if (EFI_ERROR (Status
)) {
2049 if (ProcessBuffer
!= NULL
) {
2050 FreePool (ProcessBuffer
);
2053 if (RecycleContext
!= NULL
) {
2054 FreePool (RecycleContext
);
2057 if (*RecycleEvent
!= NULL
) {
2058 gBS
->CloseEvent (*RecycleEvent
);
2066 This function processes the inbound traffic with IPsec.
2068 It checks the received packet security property, trims the ESP/AH header, and then
2069 returns without an IPsec protected IP Header and FragmentTable.
2071 @param[in] IpVersion The version of IP.
2072 @param[in, out] IpHead Points to IP header containing the ESP/AH header
2073 to be trimed on input, and without ESP/AH header
2075 @param[in, out] LastHead The Last Header in IP header on return.
2076 @param[in, out] OptionsBuffer Pointer to the options buffer.
2077 @param[in, out] OptionsLength Length of the options buffer.
2078 @param[in, out] FragmentTable Pointer to a list of fragments in form of IPsec
2079 protected on input, and without IPsec protected
2081 @param[in, out] FragmentCount The number of fragments.
2082 @param[out] SpdEntry Pointer to contain the address of SPD entry on return.
2083 @param[out] RecycleEvent The event for recycling of resources.
2085 @retval EFI_SUCCESS The operation was successful.
2086 @retval EFI_UNSUPPORTED The IPSEC protocol is not supported.
2090 IpSecProtectInboundPacket (
2092 IN OUT VOID
*IpHead
,
2093 IN OUT UINT8
*LastHead
,
2094 IN OUT VOID
**OptionsBuffer
,
2095 IN OUT UINT32
*OptionsLength
,
2096 IN OUT EFI_IPSEC_FRAGMENT_DATA
**FragmentTable
,
2097 IN OUT UINT32
*FragmentCount
,
2098 OUT EFI_IPSEC_SPD_SELECTOR
**SpdEntry
,
2099 OUT EFI_EVENT
*RecycleEvent
2102 if (*LastHead
== IPSEC_ESP_PROTOCOL
) {
2104 // Process the esp ipsec header of the inbound traffic.
2106 return IpSecEspInboundPacket (
2119 // The other protocols are not supported.
2121 return EFI_UNSUPPORTED
;
2125 This fucntion processes the output traffic with IPsec.
2127 It protected the sending packet by encrypting it payload and inserting ESP/AH header
2128 in the orginal IP header, then return the IpHeader and IPsec protected Fragmentable.
2130 @param[in] IpVersion The version of IP.
2131 @param[in, out] IpHead Point to IP header containing the orginal IP header
2132 to be processed on input, and inserted ESP/AH header
2134 @param[in, out] LastHead The Last Header in IP header.
2135 @param[in, out] OptionsBuffer Pointer to the options buffer.
2136 @param[in, out] OptionsLength Length of the options buffer.
2137 @param[in, out] FragmentTable Pointer to a list of fragments to be protected by
2138 IPsec on input, and with IPsec protected
2140 @param[in, out] FragmentCount Number of fragments.
2141 @param[in] SadEntry Related SAD entry.
2142 @param[out] RecycleEvent Event for recycling of resources.
2144 @retval EFI_SUCCESS The operation is successful.
2145 @retval EFI_UNSUPPORTED If the IPSEC protocol is not supported.
2149 IpSecProtectOutboundPacket (
2151 IN OUT VOID
*IpHead
,
2152 IN OUT UINT8
*LastHead
,
2153 IN OUT VOID
**OptionsBuffer
,
2154 IN OUT UINT32
*OptionsLength
,
2155 IN OUT EFI_IPSEC_FRAGMENT_DATA
**FragmentTable
,
2156 IN OUT UINT32
*FragmentCount
,
2157 IN IPSEC_SAD_ENTRY
*SadEntry
,
2158 OUT EFI_EVENT
*RecycleEvent
2161 if (SadEntry
->Id
->Proto
== EfiIPsecESP
) {
2163 // Process the esp ipsec header of the outbound traffic.
2165 return IpSecEspOutboundPacket (
2178 // The other protocols are not supported.
2180 return EFI_UNSUPPORTED
;