2 IP6 option support functions and routines.
4 Copyright (c) 2009 - 2010, 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.
19 Validate the IP6 option format for both the packets we received
20 and that we will transmit. It will compute the ICMPv6 error message fields
21 if the option is malformated.
23 @param[in] IpSb The IP6 service data.
24 @param[in] Packet The to be validated packet.
25 @param[in] Option The first byte of the option.
26 @param[in] OptionLen The length of the whole option.
27 @param[in] Pointer Identifies the octet offset within
28 the invoking packet where the error was detected.
31 @retval TRUE The option is properly formatted.
32 @retval FALSE The option is malformated.
49 while (Offset
< OptionLen
) {
50 OptionType
= *(Option
+ Offset
);
55 // It is a Pad1 option
61 // It is a PadN option
63 Offset
= (UINT8
) (Offset
+ *(Option
+ Offset
+ 1) + 2);
65 case Ip6OptionRouterAlert
:
67 // It is a Router Alert Option
73 // The highest-order two bits specify the action must be taken if
74 // the processing IPv6 node does not recognize the option type.
76 switch (OptionType
& Ip6OptionMask
) {
78 Offset
= (UINT8
) (Offset
+ *(Option
+ Offset
+ 1));
80 case Ip6OptionDiscard
:
82 case Ip6OptionParameterProblem
:
83 Pointer
= Pointer
+ Offset
+ sizeof (EFI_IP6_HEADER
);
88 &Packet
->Ip
.Ip6
->SourceAddress
,
89 ICMP_V6_PARAMETER_PROBLEM
,
95 if (!IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
96 Pointer
= Pointer
+ Offset
+ sizeof (EFI_IP6_HEADER
);
101 &Packet
->Ip
.Ip6
->SourceAddress
,
102 ICMP_V6_PARAMETER_PROBLEM
,
121 Validate the IP6 option format for both the packets we received
122 and that we will transmit. It supports the defined options in Neighbor
125 @param[in] Option The first byte of the option.
126 @param[in] OptionLen The length of the whole option.
128 @retval TRUE The option is properly formatted.
129 @retval FALSE The option is malformated.
144 while (Offset
< OptionLen
) {
145 OptionType
= *(Option
+ Offset
);
146 Length
= (UINT16
) (*(Option
+ Offset
+ 1) * 8);
148 switch (OptionType
) {
149 case Ip6OptionPrefixInfo
:
165 // Check the length of Ip6OptionEtherSource, Ip6OptionEtherTarget, and
166 // Ip6OptionRedirected here. For unrecognized options, silently ignore
167 // and continue processsing the message.
176 Offset
= (UINT16
) (Offset
+ Length
);
184 Validate whether the NextHeader is a known valid protocol or one of the user configured
185 protocols from the upper layer.
187 @param[in] IpSb The IP6 service instance.
188 @param[in] NextHeader The next header field.
190 @retval TRUE The NextHeader is a known valid protocol or user configured.
191 @retval FALSE The NextHeader is not a known valid protocol.
196 IN IP6_SERVICE
*IpSb
,
201 IP6_PROTOCOL
*IpInstance
;
203 if (NextHeader
== EFI_IP_PROTO_TCP
||
204 NextHeader
== EFI_IP_PROTO_UDP
||
205 NextHeader
== IP6_ICMP
||
206 NextHeader
== IP6_ESP
215 if (IpSb
->Signature
!= IP6_SERVICE_SIGNATURE
) {
219 NET_LIST_FOR_EACH (Entry
, &IpSb
->Children
) {
220 IpInstance
= NET_LIST_USER_STRUCT_S (Entry
, IP6_PROTOCOL
, Link
, IP6_PROTOCOL_SIGNATURE
);
221 if (IpInstance
->State
== IP6_STATE_CONFIGED
) {
222 if (IpInstance
->ConfigData
.DefaultProtocol
== NextHeader
) {
232 Validate the IP6 extension header format for both the packets we received
233 and that we will transmit. It will compute the ICMPv6 error message fields
234 if the option is mal-formated.
236 @param[in] IpSb The IP6 service instance. This is an optional parameter.
237 @param[in] Packet The data of the packet. Ignored if NULL.
238 @param[in] NextHeader The next header field in IPv6 basic header.
239 @param[in] ExtHdrs The first byte of the option.
240 @param[in] ExtHdrsLen The length of the whole option.
241 @param[in] Rcvd The option is from the packet we received if TRUE,
242 otherwise, the option we want to transmit.
243 @param[out] FormerHeader The offset of NextHeader which points to Fragment
244 Header when we received, of the ExtHdrs.
245 Ignored if we transmit.
246 @param[out] LastHeader The pointer of NextHeader of the last extension
247 header processed by IP6.
248 @param[out] RealExtsLen The length of extension headers processed by IP6 layer.
249 This is an optional parameter that may be NULL.
250 @param[out] UnFragmentLen The length of unfragmented length of extension headers.
251 This is an optional parameter that may be NULL.
252 @param[out] Fragmented Indicate whether the packet is fragmented.
253 This is an optional parameter that may be NULL.
255 @retval TRUE The option is properly formated.
256 @retval FALSE The option is malformated.
261 IN IP6_SERVICE
*IpSb OPTIONAL
,
262 IN NET_BUF
*Packet OPTIONAL
,
263 IN UINT8
*NextHeader
,
265 IN UINT32 ExtHdrsLen
,
267 OUT UINT32
*FormerHeader OPTIONAL
,
268 OUT UINT8
**LastHeader
,
269 OUT UINT32
*RealExtsLen OPTIONAL
,
270 OUT UINT32
*UnFragmentLen OPTIONAL
,
271 OUT BOOLEAN
*Fragmented OPTIONAL
281 IP6_FRAGMENT_HEADER
*FragmentHead
;
282 UINT16 FragmentOffset
;
283 IP6_ROUTING_HEADER
*RoutingHead
;
285 if (RealExtsLen
!= NULL
) {
289 if (UnFragmentLen
!= NULL
) {
293 if (Fragmented
!= NULL
) {
297 *LastHeader
= NextHeader
;
299 if (ExtHdrs
== NULL
&& ExtHdrsLen
== 0) {
303 if ((ExtHdrs
== NULL
&& ExtHdrsLen
!= 0) || (ExtHdrs
!= NULL
&& ExtHdrsLen
== 0)) {
313 while (Offset
<= ExtHdrsLen
) {
315 switch (*NextHeader
) {
322 // Hop-by-Hop Options header is restricted to appear immediately after an IPv6 header only.
323 // If not, generate a ICMP parameter problem message with code value of 1.
326 Pointer
= sizeof (EFI_IP6_HEADER
);
328 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
331 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
332 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
337 &Packet
->Ip
.Ip6
->SourceAddress
,
338 ICMP_V6_PARAMETER_PROBLEM
,
351 case IP6_DESTINATION
:
352 if (*NextHeader
== IP6_DESTINATION
) {
360 NextHeader
= ExtHdrs
+ Offset
;
364 Option
= ExtHdrs
+ Offset
;
365 OptionLen
= (UINT8
) ((*Option
+ 1) * 8 - 2);
369 if (IpSb
!= NULL
&& Packet
!= NULL
&& !Ip6IsOptionValid (IpSb
, Packet
, Option
, OptionLen
, Offset
)) {
373 Offset
= Offset
+ OptionLen
;
376 if (UnFragmentLen
!= NULL
) {
377 *UnFragmentLen
= Offset
;
386 NextHeader
= ExtHdrs
+ Offset
;
387 RoutingHead
= (IP6_ROUTING_HEADER
*) NextHeader
;
390 // Type 0 routing header is defined in RFC2460 and deprecated in RFC5095.
391 // Thus all routing types are processed as unrecognized.
393 if (RoutingHead
->SegmentsLeft
== 0) {
395 // Ignore the routing header and proceed to process the next header.
397 Offset
= Offset
+ (RoutingHead
->HeaderLen
+ 1) * 8;
399 if (UnFragmentLen
!= NULL
) {
400 *UnFragmentLen
= Offset
;
405 // Discard the packet and send an ICMP Parameter Problem, Code 0, message
406 // to the packet's source address, pointing to the unrecognized routing
409 Pointer
= Offset
+ 2 + sizeof (EFI_IP6_HEADER
);
410 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
411 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
416 &Packet
->Ip
.Ip6
->SourceAddress
,
417 ICMP_V6_PARAMETER_PROBLEM
,
431 // RFC2402, AH header should after fragment header.
438 // RFC2460, ICMP Parameter Problem message with code 0 should be sent
439 // if the length of a fragment is not a multiple of 8 octects and the M
440 // flag of that fragment is 1, pointing to the Payload length field of the
443 if (IpSb
!= NULL
&& Packet
!= NULL
&& (ExtHdrsLen
% 8) != 0) {
445 // Check whether it is the last fragment.
447 FragmentHead
= (IP6_FRAGMENT_HEADER
*) (ExtHdrs
+ Offset
);
448 if (FragmentHead
== NULL
) {
452 FragmentOffset
= NTOHS (FragmentHead
->FragmentOffset
);
454 if (((FragmentOffset
& 0x1) == 0x1) &&
455 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
456 Pointer
= sizeof (UINT32
);
461 &Packet
->Ip
.Ip6
->SourceAddress
,
462 ICMP_V6_PARAMETER_PROBLEM
,
470 if (Fragmented
!= NULL
) {
474 if (Rcvd
&& FormerHeader
!= NULL
) {
475 *FormerHeader
= (UINT32
) (NextHeader
- ExtHdrs
);
478 NextHeader
= ExtHdrs
+ Offset
;
487 Option
= ExtHdrs
+ Offset
;
491 // RFC2402, Payload length is specified in 32-bit words, minus "2".
493 OptionLen
= (UINT8
) ((*Option
+ 2) * 4);
494 Offset
= Offset
+ OptionLen
;
497 case IP6_NO_NEXT_HEADER
:
498 *LastHeader
= NextHeader
;
503 if (Ip6IsValidProtocol (IpSb
, *NextHeader
)) {
505 *LastHeader
= NextHeader
;
507 if (RealExtsLen
!= NULL
) {
508 *RealExtsLen
= Offset
;
515 // The Next Header value is unrecognized by the node, discard the packet and
516 // send an ICMP parameter problem message with code value of 1.
520 // The Next Header directly follows IPv6 basic header.
525 Pointer
= sizeof (EFI_IP6_HEADER
);
527 Pointer
= Offset
+ sizeof (EFI_IP6_HEADER
);
531 if ((IpSb
!= NULL
) && (Packet
!= NULL
) &&
532 !IP6_IS_MULTICAST (&Packet
->Ip
.Ip6
->DestinationAddress
)) {
537 &Packet
->Ip
.Ip6
->SourceAddress
,
538 ICMP_V6_PARAMETER_PROBLEM
,
547 *LastHeader
= NextHeader
;
549 if (RealExtsLen
!= NULL
) {
550 *RealExtsLen
= Offset
;
557 Generate an IPv6 router alert option in network order and output it through Buffer.
559 @param[out] Buffer Points to a buffer to record the generated option.
560 @param[in, out] BufferLen The length of Buffer, in bytes.
561 @param[in] NextHeader The 8-bit selector indicates the type of header
562 immediately following the Hop-by-Hop Options header.
564 @retval EFI_BUFFER_TOO_SMALL The Buffer is too small to contain the generated
565 option. BufferLen is updated for the required size.
567 @retval EFI_SUCCESS The option is generated and filled in to Buffer.
573 IN OUT UINTN
*BufferLen
,
577 UINT8 BufferArray
[8];
579 if (*BufferLen
< 8) {
581 return EFI_BUFFER_TOO_SMALL
;
585 // Form the Hop-By-Hop option in network order.
586 // NextHeader (1 octet) + HdrExtLen (1 octet) + RouterAlertOption(4 octets) + PadN
587 // The Hdr Ext Len is the length in 8-octet units, and does not including the first 8 octets.
589 ZeroMem (BufferArray
, sizeof (BufferArray
));
590 BufferArray
[0] = NextHeader
;
591 BufferArray
[2] = 0x5;
592 BufferArray
[3] = 0x2;
595 CopyMem (Buffer
, BufferArray
, sizeof (BufferArray
));
600 Insert a Fragment Header to the Extension headers and output it in UpdatedExtHdrs.
602 @param[in] IpSb The IP6 service instance to transmit the packet.
603 @param[in] NextHeader The extension header type of first extension header.
604 @param[in] LastHeader The extension header type of last extension header.
605 @param[in] ExtHdrs The length of the original extension header.
606 @param[in] ExtHdrsLen The length of the extension headers.
607 @param[in] FragmentOffset The fragment offset of the data following the header.
608 @param[out] UpdatedExtHdrs The updated ExtHdrs with Fragment header inserted.
609 It's caller's responsiblity to free this buffer.
611 @retval EFI_OUT_OF_RESOURCES Failed to finish the operation due to lake of
613 @retval EFI_UNSUPPORTED The extension header specified in ExtHdrs is not
615 @retval EFI_SUCCESS The operation performed successfully.
619 Ip6FillFragmentHeader (
620 IN IP6_SERVICE
*IpSb
,
624 IN UINT32 ExtHdrsLen
,
625 IN UINT16 FragmentOffset
,
626 OUT UINT8
**UpdatedExtHdrs
636 IP6_FRAGMENT_HEADER FragmentHead
;
638 if (UpdatedExtHdrs
== NULL
) {
639 return EFI_INVALID_PARAMETER
;
642 Length
= ExtHdrsLen
+ sizeof (IP6_FRAGMENT_HEADER
);
643 Buffer
= AllocatePool (Length
);
644 if (Buffer
== NULL
) {
645 return EFI_OUT_OF_RESOURCES
;
651 Current
= NextHeader
;
653 while ((ExtHdrs
!= NULL
) && (Offset
<= ExtHdrsLen
)) {
654 switch (NextHeader
) {
657 case IP6_DESTINATION
:
658 Current
= NextHeader
;
659 NextHeader
= *(ExtHdrs
+ Offset
);
661 if ((Current
== IP6_DESTINATION
) && (NextHeader
!= IP6_ROUTING
)) {
663 // Destination Options header should occur at most twice, once before
664 // a Routing header and once before the upper-layer header. Here we
665 // find the one before the upper-layer header. Insert the Fragment
668 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
669 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
673 Offset
= ExtHdrsLen
+ 1;
678 FormerHeader
= Offset
;
679 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 1) * 8;
680 Part1Len
= Part1Len
+ HeaderLen
;
681 Offset
= Offset
+ HeaderLen
;
685 Current
= NextHeader
;
688 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
691 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
696 Offset
= ExtHdrsLen
+ 1;
700 Current
= NextHeader
;
701 NextHeader
= *(ExtHdrs
+ Offset
);
703 // RFC2402, Payload length is specified in 32-bit words, minus "2".
705 HeaderLen
= (*(ExtHdrs
+ Offset
+ 1) + 2) * 4;
706 Part1Len
= Part1Len
+ HeaderLen
;
707 Offset
= Offset
+ HeaderLen
;
711 if (Ip6IsValidProtocol (IpSb
, NextHeader
)) {
712 Current
= NextHeader
;
713 CopyMem (Buffer
, ExtHdrs
, Part1Len
);
714 *(Buffer
+ FormerHeader
) = IP6_FRAGMENT
;
718 Offset
= ExtHdrsLen
+ 1;
723 return EFI_UNSUPPORTED
;
728 // Append the Fragment header. If the fragment offset indicates the fragment
729 // is the first fragment.
731 if ((FragmentOffset
& IP6_FRAGMENT_OFFSET_MASK
) == 0) {
732 FragmentHead
.NextHeader
= Current
;
734 FragmentHead
.NextHeader
= LastHeader
;
737 FragmentHead
.Reserved
= 0;
738 FragmentHead
.FragmentOffset
= HTONS (FragmentOffset
);
739 FragmentHead
.Identification
= mIp6Id
;
741 CopyMem (Buffer
+ Part1Len
, &FragmentHead
, sizeof (IP6_FRAGMENT_HEADER
));
743 if ((ExtHdrs
!= NULL
) && (Part1Len
< ExtHdrsLen
)) {
745 // Append the part2 (fragmentable part) of Extension headers
748 Buffer
+ Part1Len
+ sizeof (IP6_FRAGMENT_HEADER
),
750 ExtHdrsLen
- Part1Len
754 *UpdatedExtHdrs
= Buffer
;