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
,
114 Validate the IP6 option format for both the packets we received
115 and that we will transmit. It supports the defined options in Neighbor
118 @param[in] Option The first byte of the option.
119 @param[in] OptionLen The length of the whole option.
121 @retval TRUE The option is properly formatted.
122 @retval FALSE The option is malformatted.
133 IP6_OPTION_HEADER
*OptionHeader
;
135 if (Option
== NULL
) {
136 ASSERT (Option
!= NULL
);
143 // RFC 4861 states that Neighbor Discovery packet can contain zero or more
144 // options. Start processing the options if at least Type + Length fields
145 // fit within the input buffer.
147 while (Offset
+ sizeof (IP6_OPTION_HEADER
) - 1 < OptionLen
) {
148 OptionHeader
= (IP6_OPTION_HEADER
*)(Option
+ Offset
);
149 Length
= (UINT16
)OptionHeader
->Length
* 8;
151 switch (OptionHeader
->Type
) {
152 case Ip6OptionPrefixInfo
:
167 // RFC 4861 states that Length field cannot be 0.
176 // Check whether recognized options are within the input buffer's scope.
178 switch (OptionHeader
->Type
) {
179 case Ip6OptionEtherSource
:
180 case Ip6OptionEtherTarget
:
181 case Ip6OptionPrefixInfo
:
182 case Ip6OptionRedirected
:
184 if (Offset
+ Length
> (UINT32
)OptionLen
) {
192 // Unrecognized options can be either valid (but unused) or invalid
193 // (garbage in between or right after valid options). Silently ignore.
199 // Advance to the next option.
200 // Length already considers option header's Type + Length.
209 Validate whether the NextHeader is a known valid protocol or one of the user configured
210 protocols from the upper layer.
212 @param[in] IpSb The IP6 service instance.
213 @param[in] NextHeader The next header field.
215 @retval TRUE The NextHeader is a known valid protocol or user configured.
216 @retval FALSE The NextHeader is not a known valid protocol.
221 IN IP6_SERVICE
*IpSb
,
226 IP6_PROTOCOL
*IpInstance
;
228 if ((NextHeader
== EFI_IP_PROTO_TCP
) ||
229 (NextHeader
== EFI_IP_PROTO_UDP
) ||
230 (NextHeader
== IP6_ICMP
) ||
231 (NextHeader
== IP6_ESP
)
241 if (IpSb
->Signature
!= IP6_SERVICE_SIGNATURE
) {
245 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
246 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
247 if (IpInstance
->State
== IP6_STATE_CONFIGED
) {
248 if (IpInstance
->ConfigData
.DefaultProtocol
== NextHeader
) {
258 Validate the IP6 extension header format for both the packets we received
259 and that we will transmit. It will compute the ICMPv6 error message fields
260 if the option is mal-formatted.
262 @param[in] IpSb The IP6 service instance. This is an optional parameter.
263 @param[in] Packet The data of the packet. Ignored if NULL.
264 @param[in] NextHeader The next header field in IPv6 basic header.
265 @param[in] ExtHdrs The first byte of the option.
266 @param[in] ExtHdrsLen The length of the whole option.
267 @param[in] Rcvd The option is from the packet we received if TRUE,
268 otherwise, the option we want to transmit.
269 @param[out] FormerHeader The offset of NextHeader which points to Fragment
270 Header when we received, of the ExtHdrs.
271 Ignored if we transmit.
272 @param[out] LastHeader The pointer of NextHeader of the last extension
273 header processed by IP6.
274 @param[out] RealExtsLen The length of extension headers processed by IP6 layer.
275 This is an optional parameter that may be NULL.
276 @param[out] UnFragmentLen The length of unfragmented length of extension headers.
277 This is an optional parameter that may be NULL.
278 @param[out] Fragmented Indicate whether the packet is fragmented.
279 This is an optional parameter that may be NULL.
281 @retval TRUE The option is properly formatted.
282 @retval FALSE The option is malformatted.
287 IN IP6_SERVICE
*IpSb OPTIONAL
,
288 IN NET_BUF
*Packet OPTIONAL
,
289 IN UINT8
*NextHeader
,
291 IN UINT32 ExtHdrsLen
,
293 OUT UINT32
*FormerHeader OPTIONAL
,
294 OUT UINT8
**LastHeader
,
295 OUT UINT32
*RealExtsLen OPTIONAL
,
296 OUT UINT32
*UnFragmentLen OPTIONAL
,
297 OUT BOOLEAN
*Fragmented OPTIONAL
307 IP6_FRAGMENT_HEADER
*FragmentHead
;
308 UINT16 FragmentOffset
;
309 IP6_ROUTING_HEADER
*RoutingHead
;
311 if (RealExtsLen
!= NULL
) {
315 if (UnFragmentLen
!= NULL
) {
319 if (Fragmented
!= NULL
) {
323 *LastHeader
= NextHeader
;
325 if ((ExtHdrs
== NULL
) && (ExtHdrsLen
== 0)) {
329 if (((ExtHdrs
== NULL
) && (ExtHdrsLen
!= 0)) || ((ExtHdrs
!= NULL
) && (ExtHdrsLen
== 0))) {
339 while (Offset
<= ExtHdrsLen
) {
340 switch (*NextHeader
) {
348 // Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only.
349 // If not, generate a ICMP parameter problem message with code value of 1.
352 Pointer
= sizeof (EFI_IP6_HEADER
);
354 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
357 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
358 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
))
364 &Packet
->Ip
.Ip6
->SourceAddress
,
365 ICMP_V6_PARAMETER_PROBLEM
,
379 case IP6_DESTINATION
:
380 if (*NextHeader
== IP6_DESTINATION
) {
388 NextHeader
= ExtHdrs
+ Offset
;
392 Option
= ExtHdrs
+ Offset
;
393 OptionLen
= (UINT8
)((*Option
+ 1) * 8 - 2);
397 if ((IpSb
!= NULL
) && (Packet
!= NULL
) && !Ip6IsOptionValid (IpSb
, Packet
, Option
, OptionLen
, Offset
)) {
401 Offset
= Offset
+ OptionLen
;
404 if (UnFragmentLen
!= NULL
) {
405 *UnFragmentLen
= Offset
;
414 NextHeader
= ExtHdrs
+ Offset
;
415 RoutingHead
= (IP6_ROUTING_HEADER
*)NextHeader
;
418 // Type 0 routing header is defined in RFC2460 and deprecated in RFC5095.
419 // Thus all routing types are processed as unrecognized.
421 if (RoutingHead
->SegmentsLeft
== 0) {
423 // Ignore the routing header and proceed to process the next header.
425 Offset
= Offset
+ (RoutingHead
->HeaderLen
+ 1) * 8;
427 if (UnFragmentLen
!= NULL
) {
428 *UnFragmentLen
= Offset
;
432 // Discard the packet and send an ICMP Parameter Problem, Code 0, message
433 // to the packet's source address, pointing to the unrecognized routing
436 Pointer
= Offset
+ 2 + sizeof (EFI_IP6_HEADER
);
437 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
438 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
))
444 &Packet
->Ip
.Ip6
->SourceAddress
,
445 ICMP_V6_PARAMETER_PROBLEM
,
459 // RFC2402, AH header should after fragment header.
466 // RFC2460, ICMP Parameter Problem message with code 0 should be sent
467 // if the length of a fragment is not a multiple of 8 octets and the M
468 // flag of that fragment is 1, pointing to the Payload length field of the
471 if ((IpSb
!= NULL
) && (Packet
!= NULL
) && ((ExtHdrsLen
% 8) != 0)) {
473 // Check whether it is the last fragment.
475 FragmentHead
= (IP6_FRAGMENT_HEADER
*)(ExtHdrs
+ Offset
);
476 if (FragmentHead
== NULL
) {
480 FragmentOffset
= NTOHS (FragmentHead
->FragmentOffset
);
482 if (((FragmentOffset
& 0x1) == 0x1) &&
483 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
))
485 Pointer
= sizeof (UINT32
);
490 &Packet
->Ip
.Ip6
->SourceAddress
,
491 ICMP_V6_PARAMETER_PROBLEM
,
499 if (Fragmented
!= NULL
) {
503 if (Rcvd
&& (FormerHeader
!= NULL
)) {
504 *FormerHeader
= (UINT32
)(NextHeader
- ExtHdrs
);
507 NextHeader
= ExtHdrs
+ Offset
;
516 Option
= ExtHdrs
+ Offset
;
520 // RFC2402, Payload length is specified in 32-bit words, minus "2".
522 OptionLen
= (UINT8
)((*Option
+ 2) * 4);
523 Offset
= Offset
+ OptionLen
;
526 case IP6_NO_NEXT_HEADER
:
527 *LastHeader
= NextHeader
;
532 if (Ip6IsValidProtocol (IpSb
, *NextHeader
)) {
533 *LastHeader
= NextHeader
;
535 if (RealExtsLen
!= NULL
) {
536 *RealExtsLen
= Offset
;
543 // The Next Header value is unrecognized by the node, discard the packet and
544 // send an ICMP parameter problem message with code value of 1.
548 // The Next Header directly follows IPv6 basic header.
553 Pointer
= sizeof (EFI_IP6_HEADER
);
555 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
559 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
560 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
))
566 &Packet
->Ip
.Ip6
->SourceAddress
,
567 ICMP_V6_PARAMETER_PROBLEM
,
577 *LastHeader
= NextHeader
;
579 if (RealExtsLen
!= NULL
) {
580 *RealExtsLen
= Offset
;
587 Generate an IPv6 router alert option in network order and output it through Buffer.
589 @param[out] Buffer Points to a buffer to record the generated option.
590 @param[in, out] BufferLen The length of Buffer, in bytes.
591 @param[in] NextHeader The 8-bit selector indicates the type of header
592 immediately following the Hop-by-Hop Options header.
594 @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated
595 option. BufferLen is updated for the required size.
597 @retval EFI_SUCCESS The option is generated and filled in to Buffer.
603 IN OUT UINTN
*BufferLen
,
607 UINT8 BufferArray
[8];
609 if (*BufferLen
< 8) {
611 return EFI_BUFFER_TOO_SMALL
;
615 // Form the Hop-By-Hop option in network order.
616 // NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN
617 // The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets.
619 ZeroMem (BufferArray
, sizeof (BufferArray
));
620 BufferArray
[0] = NextHeader
;
621 BufferArray
[2] = 0x5;
622 BufferArray
[3] = 0x2;
625 CopyMem (Buffer
, BufferArray
, sizeof (BufferArray
));
630 Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs.
632 @param[in] IpSb The IP6 service instance to transmit the packet.
633 @param[in] NextHeader The extension header type of first extension header.
634 @param[in] LastHeader The extension header type of last extension header.
635 @param[in] ExtHdrs The length of the original extension header.
636 @param[in] ExtHdrsLen The length of the extension headers.
637 @param[in] FragmentOffset The fragment offset of the data following the header.
638 @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted.
639 It's caller's responsibility to free this buffer.
641 @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of
643 @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not
645 @retval EFI_SUCCESS The operation performed successfully.
649 Ip6FillFragmentHeader (
650 IN IP6_SERVICE
*IpSb
,
654 IN UINT32 ExtHdrsLen
,
655 IN UINT16 FragmentOffset
,
656 OUT UINT8
**UpdatedExtHdrs
666 IP6_FRAGMENT_HEADER FragmentHead
;
668 if (UpdatedExtHdrs
== NULL
) {
669 return EFI_INVALID_PARAMETER
;
672 Length
= ExtHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
);
673 Buffer
= AllocatePool (Length
);
674 if (Buffer
== NULL
) {
675 return EFI_OUT_OF_RESOURCES
;
681 Current
= NextHeader
;
683 while ((ExtHdrs
!= NULL
) && (Offset
<= ExtHdrsLen
)) {
684 switch (NextHeader
) {
687 case IP6_DESTINATION
:
688 Current
= NextHeader
;
689 NextHeader
= *(ExtHdrs
+ Offset
);
691 if ((Current
== IP6_DESTINATION
) && (NextHeader
!= IP6_ROUTING
)) {
693 // Destination Options header should occur at most twice, once before
694 // a Routing header and once before the upper-layer header. Here we
695 // find the one before the upper-layer header. Insert the Fragment
698 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
699 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
703 Offset
= ExtHdrsLen
+ 1;
707 FormerHeader
= Offset
;
708 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 1) * 8;
709 Part1Len
= Part1Len
+ HeaderLen
;
710 Offset
= Offset
+ HeaderLen
;
714 Current
= NextHeader
;
717 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
720 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
725 Offset
= ExtHdrsLen
+ 1;
729 Current
= NextHeader
;
730 NextHeader
= *(ExtHdrs
+ Offset
);
732 // RFC2402, Payload length is specified in 32-bit words, minus "2".
734 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 2) * 4;
735 Part1Len
= Part1Len
+ HeaderLen
;
736 Offset
= Offset
+ HeaderLen
;
740 if (Ip6IsValidProtocol (IpSb
, NextHeader
)) {
741 Current
= NextHeader
;
742 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
743 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
747 Offset
= ExtHdrsLen
+ 1;
752 return EFI_UNSUPPORTED
;
757 // Append the Fragment header. If the fragment offset indicates the fragment
758 // is the first fragment.
760 if ((FragmentOffset
& IP6_FRAGMENT_OFFSET_MASK
) == 0) {
761 FragmentHead
.NextHeader
= Current
;
763 FragmentHead
.NextHeader
= LastHeader
;
766 FragmentHead
.Reserved
= 0;
767 FragmentHead
.FragmentOffset
= HTONS (FragmentOffset
);
768 FragmentHead
.Identification
= mIp6Id
;
770 CopyMem (Buffer
+ Part1Len
, &FragmentHead
, sizeof (IP6_FRAGMENT_HEADER
));
772 if ((ExtHdrs
!= NULL
) && (Part1Len
< ExtHdrsLen
)) {
774 // Append the part2 (fragmentable part) of Extension headers
777 Buffer
+ Part1Len
+ sizeof (IP6_FRAGMENT_HEADER
),
779 ExtHdrsLen
- Part1Len
783 *UpdatedExtHdrs
= Buffer
;