]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Option.c
2 IP4 option support functions.
4 Copyright (c) 2005 - 2006, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Validate the IP4 option format for both the packets we received
20 and will transmit. It will compute the ICMP error message fields
21 if the option is mal-formated. But this information isn't used.
23 @param[in] Option The first byte of the option
24 @param[in] OptionLen The length of the whole option
25 @param[in] Rcvd The option is from the packet we received if TRUE,
26 otherwise the option we wants to transmit.
28 @retval TRUE The option is properly formatted
29 @retval FALSE The option is mal-formated
42 volatile UINT8 IcmpType
;
43 volatile UINT8 IcmpCode
;
44 volatile UINT32 IcmpPoint
;
46 IcmpType
= ICMP_PARAMETER_PROBLEM
;
52 while (Cur
< OptionLen
) {
53 switch (Option
[Cur
]) {
65 Len
= Option
[Cur
+ 1];
66 Point
= Option
[Cur
+ 2];
69 // SRR/RR options are formatted as |Type|Len|Point|Ip1|Ip2|...
71 if ((OptionLen
- Cur
< Len
) || (Len
< 3) || ((Len
- 3) % 4 != 0)) {
76 if ((Point
> Len
+ 1) || (Point
% 4 != 0)) {
82 // The Point must point pass the last entry if the packet is received
83 // by us. It must point to 4 if the packet is to be sent by us for
84 // source route option.
86 if ((Option
[Cur
] != IP4_OPTION_RR
) &&
87 ((Rcvd
&& (Point
!= Len
+ 1)) || (!Rcvd
&& (Point
!= 4)))) {
89 IcmpType
= ICMP_DEST_UNREACHABLE
;
90 IcmpCode
= ICMP_SOURCEROUTE_FAILED
;
98 Len
= Option
[Cur
+ 1];
100 if ((OptionLen
- Cur
< Len
) || (Len
< 2)) {
116 Copy the option from the original option to buffer. It
117 handles the details such as:
118 1. whether copy the single IP4 option to the first/non-first
120 2. Pad the options copied over to aligned to 4 bytes.
122 @param[in] Option The original option to copy from
123 @param[in] OptionLen The length of the original option
124 @param[in] FirstFragment Whether it is the first fragment
125 @param[in, out] Buf The buffer to copy options to. NULL
126 @param[in, out] BufLen The length of the buffer
128 @retval EFI_SUCCESS The options are copied over
129 @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small.
136 IN BOOLEAN FirstFragment
,
137 IN OUT UINT8
*Buf
, OPTIONAL
138 IN OUT UINT32
*BufLen
147 ASSERT ((BufLen
!= NULL
) && (OptionLen
<= 40));
152 while (Cur
< OptionLen
) {
154 Len
= Option
[Cur
+ 1];
156 if (Type
== IP4_OPTION_NOP
) {
158 // Keep the padding, in case that the sender wants to align
159 // the option, say, to 4 bytes
161 OptBuf
[Next
] = IP4_OPTION_NOP
;
165 } else if (Type
== IP4_OPTION_EOP
) {
167 // Don't append the EOP to avoid including only a EOP option
173 // don't copy options that is only valid for the first fragment
175 if (FirstFragment
|| (Type
& IP4_OPTION_COPY_MASK
) != 0) {
176 CopyMem (OptBuf
+ Next
, Option
+ Cur
, Len
);
185 // Don't append an EOP only option.
193 // Append an EOP if the end of option doesn't coincide with the
194 // end of the IP header, that is, isn't aligned to 4 bytes..
196 if ((Next
% 4) != 0) {
197 OptBuf
[Next
] = IP4_OPTION_EOP
;
202 // Head length is in the unit of 4 bytes. Now, Len is the
203 // acutal option length to appear in the IP header.
205 Len
= ((Next
+ 3) &~0x03);
208 // If the buffer is too small, set the BufLen then return
210 if ((Buf
== NULL
) || (*BufLen
< Len
)) {
212 return EFI_BUFFER_TOO_SMALL
;
216 // Copy the option to the Buf, zero the buffer first to pad
217 // the options with NOP to align to 4 bytes.
220 CopyMem (Buf
, OptBuf
, Next
);