]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Option.c
3 Copyright (c) 2005 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 IP4 option support functions
27 Validate the IP4 option format for both the packets we received
28 and will transmit. It will compute the ICMP error message fields
29 if the option is mal-formated. But this information isn't used.
31 @param Option The first byte of the option
32 @param OptionLen The length of the whole option
33 @param Rcvd The option is from the packet we received if TRUE,
34 otherwise the option we wants to transmit.
36 @retval TRUE The option is properly formatted
37 @retval FALSE The option is mal-formated
50 volatile UINT8 IcmpType
;
51 volatile UINT8 IcmpCode
;
52 volatile UINT32 IcmpPoint
;
54 IcmpType
= ICMP_PARAMETER_PROBLEM
;
60 while (Cur
< OptionLen
) {
61 switch (Option
[Cur
]) {
73 Len
= Option
[Cur
+ 1];
74 Point
= Option
[Cur
+ 2];
77 // SRR/RR options are formatted as |Type|Len|Point|Ip1|Ip2|...
79 if ((OptionLen
- Cur
< Len
) || (Len
< 3) || ((Len
- 3) % 4 != 0)) {
84 if ((Point
> Len
+ 1) || (Point
% 4 != 0)) {
90 // The Point must point pass the last entry if the packet is received
91 // by us. It must point to 4 if the packet is to be sent by us for
92 // source route option.
94 if ((Option
[Cur
] != IP4_OPTION_RR
) &&
95 ((Rcvd
&& (Point
!= Len
+ 1)) || (!Rcvd
&& (Point
!= 4)))) {
97 IcmpType
= ICMP_DEST_UNREACHABLE
;
98 IcmpCode
= ICMP_SOURCEROUTE_FAILED
;
106 Len
= Option
[Cur
+ 1];
108 if ((OptionLen
- Cur
< Len
) || (Len
< 2)) {
124 Copy the option from the original option to buffer. It
125 handles the details such as:
126 1. whether copy the single IP4 option to the first/non-first
128 2. Pad the options copied over to aligned to 4 bytes.
130 @param Option The original option to copy from
131 @param OptionLen The length of the original option
132 @param FirstFragment Whether it is the first fragment
133 @param Buf The buffer to copy options to. NULL
134 @param BufLen The length of the buffer
136 @retval EFI_SUCCESS The options are copied over
137 @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small.
144 IN BOOLEAN FirstFragment
,
145 IN OUT UINT8
*Buf
, OPTIONAL
146 IN OUT UINT32
*BufLen
155 ASSERT ((BufLen
!= NULL
) && (OptionLen
<= 40));
160 while (Cur
< OptionLen
) {
162 Len
= Option
[Cur
+ 1];
164 if (Type
== IP4_OPTION_NOP
) {
166 // Keep the padding, in case that the sender wants to align
167 // the option, say, to 4 bytes
169 OptBuf
[Next
] = IP4_OPTION_NOP
;
173 } else if (Type
== IP4_OPTION_EOP
) {
175 // Don't append the EOP to avoid including only a EOP option
181 // don't copy options that is only valid for the first fragment
183 if (FirstFragment
|| (Type
& IP4_OPTION_COPY_MASK
) != 0) {
184 CopyMem (OptBuf
+ Next
, Option
+ Cur
, Len
);
193 // Don't append an EOP only option.
201 // Append an EOP if the end of option doesn't coincide with the
202 // end of the IP header, that is, isn't aligned to 4 bytes..
204 if ((Next
% 4) != 0) {
205 OptBuf
[Next
] = IP4_OPTION_EOP
;
210 // Head length is in the unit of 4 bytes. Now, Len is the
211 // acutal option length to appear in the IP header.
213 Len
= ((Next
+ 3) &~0x03);
216 // If the buffer is too small, set the BufLen then return
218 if ((Buf
== NULL
) || (*BufLen
< Len
)) {
220 return EFI_BUFFER_TOO_SMALL
;
224 // Copy the option to the Buf, zero the buffer first to pad
225 // the options with NOP to align to 4 bytes.
228 CopyMem (Buf
, OptBuf
, Next
);