/** @file
IP6 option support functions and routines.
Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "Ip6Impl.h"
/**
Validate the IP6 option format for both the packets we received
and that we will transmit. It will compute the ICMPv6 error message fields
if the option is malformated.
@param[in] IpSb The IP6 service data.
@param[in] Packet The to be validated packet.
@param[in] Option The first byte of the option.
@param[in] OptionLen The length of the whole option.
@param[in] Pointer Identifies the octet offset within
the invoking packet where the error was detected.
@retval TRUE The option is properly formatted.
@retval FALSE The option is malformated.
**/
)
{
Offset = 0;
switch (OptionType) {
case Ip6OptionPad1:
//
// It is a Pad1 option
//
Offset++;
break;
case Ip6OptionPadN:
//
// It is a PadN option
//
break;
case Ip6OptionRouterAlert:
//
// It is a Router Alert Option
//
Offset += 4;
break;
default:
//
// The highest-order two bits specify the action must be taken if
// the processing IPv6 node does not recognize the option type.
//
switch (OptionType & Ip6OptionMask) {
case Ip6OptionSkip:
break;
case Ip6OptionDiscard:
return FALSE;
IpSb,
NULL,
2,
);
return FALSE;
case Ip6OptionMask:
IpSb,
NULL,
2,
);
}
return FALSE;
break;
}
break;
}
}
return TRUE;
}
/**
Validate the IP6 option format for both the packets we received
and that we will transmit. It supports the defined options in Neighbor
Discovery messages.
@param[in] Option The first byte of the option.
@param[in] OptionLen The length of the whole option.
@retval TRUE The option is properly formatted.
@retval FALSE The option is malformated.
**/
)
{
Offset = 0;
switch (OptionType) {
case Ip6OptionPrefixInfo:
if (Length != 32) {
return FALSE;
}
break;
case Ip6OptionMtu:
if (Length != 8) {
return FALSE;
}
break;
default:
//
// Check the length of Ip6OptionEtherSource, Ip6OptionEtherTarget, and
// Ip6OptionRedirected here. For unrecognized options, silently ignore
// and continue processsing the message.
//
if (Length == 0) {
return FALSE;
}
break;
}
}
return TRUE;
}
/**
Validate whether the NextHeader is a known valid protocol or one of the user configured
protocols from the upper layer.
@param[in] IpSb The IP6 service instance.
@param[in] NextHeader The next header field.
@retval TRUE The NextHeader is a known valid protocol or user configured.
@retval FALSE The NextHeader is not a known valid protocol.
**/
)
{
if (NextHeader == EFI_IP_PROTO_TCP ||
NextHeader == EFI_IP_PROTO_UDP ||
NextHeader == IP6_ICMP ||
) {
return TRUE;
}
return FALSE;
}
return FALSE;
}
return TRUE;
}
}
}
return FALSE;
}
/**
Validate the IP6 extension header format for both the packets we received
and that we will transmit. It will compute the ICMPv6 error message fields
if the option is mal-formated.
@param[in] IpSb The IP6 service instance. This is an optional parameter.
@param[in] Packet The data of the packet. Ignored if NULL.
@param[in] NextHeader The next header field in IPv6 basic header.
@param[in] ExtHdrs The first byte of the option.
@param[in] ExtHdrsLen The length of the whole option.
@param[in] Rcvd The option is from the packet we received if TRUE,
otherwise, the option we want to transmit.
@param[out] FormerHeader The offset of NextHeader which points to Fragment
Header when we received, of the ExtHdrs.
Ignored if we transmit.
@param[out] LastHeader The pointer of NextHeader of the last extension
header processed by IP6.
@param[out] RealExtsLen The length of extension headers processed by IP6 layer.
This is an optional parameter that may be NULL.
@param[out] UnFragmentLen The length of unfragmented length of extension headers.
This is an optional parameter that may be NULL.
@param[out] Fragmented Indicate whether the packet is fragmented.
This is an optional parameter that may be NULL.
@retval TRUE The option is properly formated.
@retval FALSE The option is malformated.
**/
)
{
if (RealExtsLen != NULL) {
*RealExtsLen = 0;
}
if (UnFragmentLen != NULL) {
*UnFragmentLen = 0;
}
if (Fragmented != NULL) {
*Fragmented = FALSE;
}
*LastHeader = NextHeader;
return TRUE;
}
return FALSE;
}
Pointer = 0;
Offset = 0;
CountD = 0;
CountA = 0;
while (Offset <= ExtHdrsLen) {
switch (*NextHeader) {
case IP6_HOP_BY_HOP:
if (Offset != 0) {
if (!Rcvd) {
return FALSE;
}
//
// Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only.
// If not, generate a ICMP parameter problem message with code value of 1.
//
if (Pointer == 0) {
Pointer = sizeof (EFI_IP6_HEADER);
} else {
}
IpSb,
NULL,
1,
);
}
return FALSE;
}
//
// Fall through
//
case IP6_DESTINATION:
if (*NextHeader == IP6_DESTINATION) {
CountD++;
}
if (CountD > 2) {
return FALSE;
}
Offset++;
Option++;
Offset++;
if (IpSb != NULL && Packet != NULL && !Ip6IsOptionValid (IpSb, Packet, Option, OptionLen, Offset)) {
return FALSE;
}
if (Flag) {
if (UnFragmentLen != NULL) {
*UnFragmentLen = Offset;
}
}
break;
case IP6_ROUTING:
//
// Type 0 routing header is defined in RFC2460 and deprecated in RFC5095.
// Thus all routing types are processed as unrecognized.
//
if (RoutingHead->SegmentsLeft == 0) {
//
// Ignore the routing header and proceed to process the next header.
//
if (UnFragmentLen != NULL) {
*UnFragmentLen = Offset;
}
} else {
//
// Discard the packet and send an ICMP Parameter Problem, Code 0, message
// to the packet's source address, pointing to the unrecognized routing
// type.
//
IpSb,
NULL,
0,
);
}
return FALSE;
}
break;
case IP6_FRAGMENT:
//
// RFC2402, AH header should after fragment header.
//
if (CountA > 1) {
return FALSE;
}
//
// RFC2460, ICMP Parameter Problem message with code 0 should be sent
// if the length of a fragment is not a multiple of 8 octects and the M
// flag of that fragment is 1, pointing to the Payload length field of the
// fragment packet.
//
//
// Check whether it is the last fragment.
//
if (FragmentHead == NULL) {
return FALSE;
}
IpSb,
NULL,
0,
);
return FALSE;
}
}
if (Fragmented != NULL) {
*Fragmented = TRUE;
}
}
break;
case IP6_AH:
if (++CountA > 1) {
return FALSE;
}
NextHeader = Option;
Option++;
//
// RFC2402, Payload length is specified in 32-bit words, minus "2".
//
break;
case IP6_NO_NEXT_HEADER:
*LastHeader = NextHeader;
return FALSE;
break;
default:
*LastHeader = NextHeader;
if (RealExtsLen != NULL) {
*RealExtsLen = Offset;
}
return TRUE;
}
//
// The Next Header value is unrecognized by the node, discard the packet and
// send an ICMP parameter problem message with code value of 1.
//
if (Offset == 0) {
//
// The Next Header directly follows IPv6 basic header.
//
Pointer = 6;
} else {
if (Pointer == 0) {
Pointer = sizeof (EFI_IP6_HEADER);
} else {
}
}
IpSb,
NULL,
1,
);
}
return FALSE;
}
}
*LastHeader = NextHeader;
if (RealExtsLen != NULL) {
*RealExtsLen = Offset;
}
return TRUE;
}
/**
Generate an IPv6 router alert option in network order and output it through Buffer.
@param[out] Buffer Points to a buffer to record the generated option.
@param[in, out] BufferLen The length of Buffer, in bytes.
@param[in] NextHeader The 8-bit selector indicates the type of header
immediately following the Hop-by-Hop Options header.
@retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated
option. BufferLen is updated for the required size.
@retval EFI_SUCCESS The option is generated and filled in to Buffer.
**/
)
{
if (*BufferLen < 8) {
*BufferLen = 8;
return EFI_BUFFER_TOO_SMALL;
}
//
// Form the Hop-By-Hop option in network order.
// NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN
// The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets.
//
BufferArray[0] = NextHeader;
return EFI_SUCCESS;
}
/**
Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs.
@param[in] IpSb The IP6 service instance to transmit the packet.
@param[in] NextHeader The extension header type of first extension header.
@param[in] LastHeader The extension header type of last extension header.
@param[in] ExtHdrs The length of the original extension header.
@param[in] ExtHdrsLen The length of the extension headers.
@param[in] FragmentOffset The fragment offset of the data following the header.
@param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted.
It's caller's responsiblity to free this buffer.
@retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of
resource.
@retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not
supported currently.
@retval EFI_SUCCESS The operation performed successfully.
**/
)
{
if (UpdatedExtHdrs == NULL) {
return EFI_INVALID_PARAMETER;
}
return EFI_OUT_OF_RESOURCES;
}
Offset = 0;
Part1Len = 0;
FormerHeader = 0;
switch (NextHeader) {
case IP6_ROUTING:
case IP6_HOP_BY_HOP:
case IP6_DESTINATION:
//
// Destination Options header should occur at most twice, once before
// a Routing header and once before the upper-layer header. Here we
// find the one before the upper-layer header. Insert the Fragment
// Header before it.
//
//
// Exit the loop.
//
break;
}
break;
case IP6_FRAGMENT:
if (Part1Len != 0) {
}
//
// Exit the loop.
//
break;
case IP6_AH:
//
// RFC2402, Payload length is specified in 32-bit words, minus "2".
//
break;
default:
//
// Exit the loop.
//
break;
}
return EFI_UNSUPPORTED;
}
}
//
// Append the Fragment header. If the fragment offset indicates the fragment
// is the first fragment.
//
if ((FragmentOffset & IP6_FRAGMENT_OFFSET_MASK) == 0) {
} else {
}
FragmentHead.Reserved = 0;
//
// Append the part2 (fragmentable part) of Extension headers
//
CopyMem (
);
}
*UpdatedExtHdrs = Buffer;
return EFI_SUCCESS;
}