2 IP6 option support functions and routines.
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Validate the IP6 option format for both the packets we received
14 and that we will transmit. It will compute the ICMPv6 error message fields
15 if the option is malformatted.
17 @param[in] IpSb The IP6 service data.
18 @param[in] Packet The to be validated packet.
19 @param[in] Option The first byte of the option.
20 @param[in] OptionLen The length of the whole option.
21 @param[in] Pointer Identifies the octet offset within
22 the invoking packet where the error was detected.
25 @retval TRUE The option is properly formatted.
26 @retval FALSE The option is malformatted.
43 while (Offset
< OptionLen
) {
44 OptionType
= *(Option
+ Offset
);
49 // It is a Pad1 option
55 // It is a PadN option
57 Offset
= (UINT8
) (Offset
+ *(Option
+ Offset
+ 1) + 2);
59 case Ip6OptionRouterAlert
:
61 // It is a Router Alert Option
67 // The highest-order two bits specify the action must be taken if
68 // the processing IPv6 node does not recognize the option type.
70 switch (OptionType
& Ip6OptionMask
) {
72 Offset
= (UINT8
) (Offset
+ *(Option
+ Offset
+ 1));
74 case Ip6OptionDiscard
:
76 case Ip6OptionParameterProblem
:
77 Pointer
= Pointer
+ Offset
+ sizeof (EFI_IP6_HEADER
);
82 &Packet
->Ip
.Ip6
->SourceAddress
,
83 ICMP_V6_PARAMETER_PROBLEM
,
89 if (!IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
90 Pointer
= Pointer
+ Offset
+ sizeof (EFI_IP6_HEADER
);
95 &Packet
->Ip
.Ip6
->SourceAddress
,
96 ICMP_V6_PARAMETER_PROBLEM
,
115 Validate the IP6 option format for both the packets we received
116 and that we will transmit. It supports the defined options in Neighbor
119 @param[in] Option The first byte of the option.
120 @param[in] OptionLen The length of the whole option.
122 @retval TRUE The option is properly formatted.
123 @retval FALSE The option is malformatted.
134 IP6_OPTION_HEADER
*OptionHeader
;
136 if (Option
== NULL
) {
137 ASSERT (Option
!= NULL
);
144 // RFC 4861 states that Neighbor Discovery packet can contain zero or more
145 // options. Start processing the options if at least Type + Length fields
146 // fit within the input buffer.
148 while (Offset
+ sizeof (IP6_OPTION_HEADER
) - 1 < OptionLen
) {
149 OptionHeader
= (IP6_OPTION_HEADER
*) (Option
+ Offset
);
150 Length
= (UINT16
) OptionHeader
->Length
* 8;
152 switch (OptionHeader
->Type
) {
153 case Ip6OptionPrefixInfo
:
166 // RFC 4861 states that Length field cannot be 0.
174 // Check whether recognized options are within the input buffer's scope.
176 switch (OptionHeader
->Type
) {
177 case Ip6OptionEtherSource
:
178 case Ip6OptionEtherTarget
:
179 case Ip6OptionPrefixInfo
:
180 case Ip6OptionRedirected
:
182 if (Offset
+ Length
> (UINT32
) OptionLen
) {
189 // Unrecognized options can be either valid (but unused) or invalid
190 // (garbage in between or right after valid options). Silently ignore.
196 // Advance to the next option.
197 // Length already considers option header's Type + Length.
207 Validate whether the NextHeader is a known valid protocol or one of the user configured
208 protocols from the upper layer.
210 @param[in] IpSb The IP6 service instance.
211 @param[in] NextHeader The next header field.
213 @retval TRUE The NextHeader is a known valid protocol or user configured.
214 @retval FALSE The NextHeader is not a known valid protocol.
219 IN IP6_SERVICE
*IpSb
,
224 IP6_PROTOCOL
*IpInstance
;
226 if (NextHeader
== EFI_IP_PROTO_TCP
||
227 NextHeader
== EFI_IP_PROTO_UDP
||
228 NextHeader
== IP6_ICMP
||
229 NextHeader
== IP6_ESP
238 if (IpSb
->Signature
!= IP6_SERVICE_SIGNATURE
) {
242 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
243 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
244 if (IpInstance
->State
== IP6_STATE_CONFIGED
) {
245 if (IpInstance
->ConfigData
.DefaultProtocol
== NextHeader
) {
255 Validate the IP6 extension header format for both the packets we received
256 and that we will transmit. It will compute the ICMPv6 error message fields
257 if the option is mal-formatted.
259 @param[in] IpSb The IP6 service instance. This is an optional parameter.
260 @param[in] Packet The data of the packet. Ignored if NULL.
261 @param[in] NextHeader The next header field in IPv6 basic header.
262 @param[in] ExtHdrs The first byte of the option.
263 @param[in] ExtHdrsLen The length of the whole option.
264 @param[in] Rcvd The option is from the packet we received if TRUE,
265 otherwise, the option we want to transmit.
266 @param[out] FormerHeader The offset of NextHeader which points to Fragment
267 Header when we received, of the ExtHdrs.
268 Ignored if we transmit.
269 @param[out] LastHeader The pointer of NextHeader of the last extension
270 header processed by IP6.
271 @param[out] RealExtsLen The length of extension headers processed by IP6 layer.
272 This is an optional parameter that may be NULL.
273 @param[out] UnFragmentLen The length of unfragmented length of extension headers.
274 This is an optional parameter that may be NULL.
275 @param[out] Fragmented Indicate whether the packet is fragmented.
276 This is an optional parameter that may be NULL.
278 @retval TRUE The option is properly formatted.
279 @retval FALSE The option is malformatted.
284 IN IP6_SERVICE
*IpSb OPTIONAL
,
285 IN NET_BUF
*Packet OPTIONAL
,
286 IN UINT8
*NextHeader
,
288 IN UINT32 ExtHdrsLen
,
290 OUT UINT32
*FormerHeader OPTIONAL
,
291 OUT UINT8
**LastHeader
,
292 OUT UINT32
*RealExtsLen OPTIONAL
,
293 OUT UINT32
*UnFragmentLen OPTIONAL
,
294 OUT BOOLEAN
*Fragmented OPTIONAL
304 IP6_FRAGMENT_HEADER
*FragmentHead
;
305 UINT16 FragmentOffset
;
306 IP6_ROUTING_HEADER
*RoutingHead
;
308 if (RealExtsLen
!= NULL
) {
312 if (UnFragmentLen
!= NULL
) {
316 if (Fragmented
!= NULL
) {
320 *LastHeader
= NextHeader
;
322 if (ExtHdrs
== NULL
&& ExtHdrsLen
== 0) {
326 if ((ExtHdrs
== NULL
&& ExtHdrsLen
!= 0) || (ExtHdrs
!= NULL
&& ExtHdrsLen
== 0)) {
336 while (Offset
<= ExtHdrsLen
) {
338 switch (*NextHeader
) {
345 // Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only.
346 // If not, generate a ICMP parameter problem message with code value of 1.
349 Pointer
= sizeof (EFI_IP6_HEADER
);
351 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
354 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
355 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
360 &Packet
->Ip
.Ip6
->SourceAddress
,
361 ICMP_V6_PARAMETER_PROBLEM
,
374 case IP6_DESTINATION
:
375 if (*NextHeader
== IP6_DESTINATION
) {
383 NextHeader
= ExtHdrs
+ Offset
;
387 Option
= ExtHdrs
+ Offset
;
388 OptionLen
= (UINT8
) ((*Option
+ 1) * 8 - 2);
392 if (IpSb
!= NULL
&& Packet
!= NULL
&& !Ip6IsOptionValid (IpSb
, Packet
, Option
, OptionLen
, Offset
)) {
396 Offset
= Offset
+ OptionLen
;
399 if (UnFragmentLen
!= NULL
) {
400 *UnFragmentLen
= Offset
;
409 NextHeader
= ExtHdrs
+ Offset
;
410 RoutingHead
= (IP6_ROUTING_HEADER
*) NextHeader
;
413 // Type 0 routing header is defined in RFC2460 and deprecated in RFC5095.
414 // Thus all routing types are processed as unrecognized.
416 if (RoutingHead
->SegmentsLeft
== 0) {
418 // Ignore the routing header and proceed to process the next header.
420 Offset
= Offset
+ (RoutingHead
->HeaderLen
+ 1) * 8;
422 if (UnFragmentLen
!= NULL
) {
423 *UnFragmentLen
= Offset
;
428 // Discard the packet and send an ICMP Parameter Problem, Code 0, message
429 // to the packet's source address, pointing to the unrecognized routing
432 Pointer
= Offset
+ 2 + sizeof (EFI_IP6_HEADER
);
433 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
434 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
439 &Packet
->Ip
.Ip6
->SourceAddress
,
440 ICMP_V6_PARAMETER_PROBLEM
,
454 // RFC2402, AH header should after fragment header.
461 // RFC2460, ICMP Parameter Problem message with code 0 should be sent
462 // if the length of a fragment is not a multiple of 8 octets and the M
463 // flag of that fragment is 1, pointing to the Payload length field of the
466 if (IpSb
!= NULL
&& Packet
!= NULL
&& (ExtHdrsLen
% 8) != 0) {
468 // Check whether it is the last fragment.
470 FragmentHead
= (IP6_FRAGMENT_HEADER
*) (ExtHdrs
+ Offset
);
471 if (FragmentHead
== NULL
) {
475 FragmentOffset
= NTOHS (FragmentHead
->FragmentOffset
);
477 if (((FragmentOffset
& 0x1) == 0x1) &&
478 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
479 Pointer
= sizeof (UINT32
);
484 &Packet
->Ip
.Ip6
->SourceAddress
,
485 ICMP_V6_PARAMETER_PROBLEM
,
493 if (Fragmented
!= NULL
) {
497 if (Rcvd
&& FormerHeader
!= NULL
) {
498 *FormerHeader
= (UINT32
) (NextHeader
- ExtHdrs
);
501 NextHeader
= ExtHdrs
+ Offset
;
510 Option
= ExtHdrs
+ Offset
;
514 // RFC2402, Payload length is specified in 32-bit words, minus "2".
516 OptionLen
= (UINT8
) ((*Option
+ 2) * 4);
517 Offset
= Offset
+ OptionLen
;
520 case IP6_NO_NEXT_HEADER
:
521 *LastHeader
= NextHeader
;
526 if (Ip6IsValidProtocol (IpSb
, *NextHeader
)) {
528 *LastHeader
= NextHeader
;
530 if (RealExtsLen
!= NULL
) {
531 *RealExtsLen
= Offset
;
538 // The Next Header value is unrecognized by the node, discard the packet and
539 // send an ICMP parameter problem message with code value of 1.
543 // The Next Header directly follows IPv6 basic header.
548 Pointer
= sizeof (EFI_IP6_HEADER
);
550 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
554 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
555 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
560 &Packet
->Ip
.Ip6
->SourceAddress
,
561 ICMP_V6_PARAMETER_PROBLEM
,
570 *LastHeader
= NextHeader
;
572 if (RealExtsLen
!= NULL
) {
573 *RealExtsLen
= Offset
;
580 Generate an IPv6 router alert option in network order and output it through Buffer.
582 @param[out] Buffer Points to a buffer to record the generated option.
583 @param[in, out] BufferLen The length of Buffer, in bytes.
584 @param[in] NextHeader The 8-bit selector indicates the type of header
585 immediately following the Hop-by-Hop Options header.
587 @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated
588 option. BufferLen is updated for the required size.
590 @retval EFI_SUCCESS The option is generated and filled in to Buffer.
596 IN OUT UINTN
*BufferLen
,
600 UINT8 BufferArray
[8];
602 if (*BufferLen
< 8) {
604 return EFI_BUFFER_TOO_SMALL
;
608 // Form the Hop-By-Hop option in network order.
609 // NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN
610 // The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets.
612 ZeroMem (BufferArray
, sizeof (BufferArray
));
613 BufferArray
[0] = NextHeader
;
614 BufferArray
[2] = 0x5;
615 BufferArray
[3] = 0x2;
618 CopyMem (Buffer
, BufferArray
, sizeof (BufferArray
));
623 Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs.
625 @param[in] IpSb The IP6 service instance to transmit the packet.
626 @param[in] NextHeader The extension header type of first extension header.
627 @param[in] LastHeader The extension header type of last extension header.
628 @param[in] ExtHdrs The length of the original extension header.
629 @param[in] ExtHdrsLen The length of the extension headers.
630 @param[in] FragmentOffset The fragment offset of the data following the header.
631 @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted.
632 It's caller's responsibility to free this buffer.
634 @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of
636 @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not
638 @retval EFI_SUCCESS The operation performed successfully.
642 Ip6FillFragmentHeader (
643 IN IP6_SERVICE
*IpSb
,
647 IN UINT32 ExtHdrsLen
,
648 IN UINT16 FragmentOffset
,
649 OUT UINT8
**UpdatedExtHdrs
659 IP6_FRAGMENT_HEADER FragmentHead
;
661 if (UpdatedExtHdrs
== NULL
) {
662 return EFI_INVALID_PARAMETER
;
665 Length
= ExtHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
);
666 Buffer
= AllocatePool (Length
);
667 if (Buffer
== NULL
) {
668 return EFI_OUT_OF_RESOURCES
;
674 Current
= NextHeader
;
676 while ((ExtHdrs
!= NULL
) && (Offset
<= ExtHdrsLen
)) {
677 switch (NextHeader
) {
680 case IP6_DESTINATION
:
681 Current
= NextHeader
;
682 NextHeader
= *(ExtHdrs
+ Offset
);
684 if ((Current
== IP6_DESTINATION
) && (NextHeader
!= IP6_ROUTING
)) {
686 // Destination Options header should occur at most twice, once before
687 // a Routing header and once before the upper-layer header. Here we
688 // find the one before the upper-layer header. Insert the Fragment
691 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
692 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
696 Offset
= ExtHdrsLen
+ 1;
701 FormerHeader
= Offset
;
702 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 1) * 8;
703 Part1Len
= Part1Len
+ HeaderLen
;
704 Offset
= Offset
+ HeaderLen
;
708 Current
= NextHeader
;
711 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
714 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
719 Offset
= ExtHdrsLen
+ 1;
723 Current
= NextHeader
;
724 NextHeader
= *(ExtHdrs
+ Offset
);
726 // RFC2402, Payload length is specified in 32-bit words, minus "2".
728 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 2) * 4;
729 Part1Len
= Part1Len
+ HeaderLen
;
730 Offset
= Offset
+ HeaderLen
;
734 if (Ip6IsValidProtocol (IpSb
, NextHeader
)) {
735 Current
= NextHeader
;
736 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
737 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
741 Offset
= ExtHdrsLen
+ 1;
746 return EFI_UNSUPPORTED
;
751 // Append the Fragment header. If the fragment offset indicates the fragment
752 // is the first fragment.
754 if ((FragmentOffset
& IP6_FRAGMENT_OFFSET_MASK
) == 0) {
755 FragmentHead
.NextHeader
= Current
;
757 FragmentHead
.NextHeader
= LastHeader
;
760 FragmentHead
.Reserved
= 0;
761 FragmentHead
.FragmentOffset
= HTONS (FragmentOffset
);
762 FragmentHead
.Identification
= mIp6Id
;
764 CopyMem (Buffer
+ Part1Len
, &FragmentHead
, sizeof (IP6_FRAGMENT_HEADER
));
766 if ((ExtHdrs
!= NULL
) && (Part1Len
< ExtHdrsLen
)) {
768 // Append the part2 (fragmentable part) of Extension headers
771 Buffer
+ Part1Len
+ sizeof (IP6_FRAGMENT_HEADER
),
773 ExtHdrsLen
- Part1Len
777 *UpdatedExtHdrs
= Buffer
;