]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Option.c
IntelFrameworkModulePkg: Remove variables that are set, but not used
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Option.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 IP4 option support functions.\r
3 \r
e5eed7d3
HT
4Copyright (c) 2005 - 2006, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
772db4bb 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
772db4bb 13**/\r
14\r
15#include "Ip4Impl.h"\r
16\r
17\r
18/**\r
19 Validate the IP4 option format for both the packets we received\r
20 and will transmit. It will compute the ICMP error message fields\r
21 if the option is mal-formated. But this information isn't used.\r
22\r
3e8c18da 23 @param[in] Option The first byte of the option\r
24 @param[in] OptionLen The length of the whole option\r
25 @param[in] Rcvd The option is from the packet we received if TRUE,\r
772db4bb 26 otherwise the option we wants to transmit.\r
27\r
96e1079f 28 @retval TRUE The option is properly formatted\r
29 @retval FALSE The option is mal-formated\r
772db4bb 30\r
31**/\r
32BOOLEAN\r
33Ip4OptionIsValid (\r
34 IN UINT8 *Option,\r
35 IN UINT32 OptionLen,\r
36 IN BOOLEAN Rcvd\r
37 )\r
38{\r
39 UINT32 Cur;\r
40 UINT32 Len;\r
41 UINT32 Point;\r
687a2e5f 42 volatile UINT8 IcmpType;\r
43 volatile UINT8 IcmpCode;\r
44 volatile UINT32 IcmpPoint;\r
772db4bb 45\r
46 IcmpType = ICMP_PARAMETER_PROBLEM;\r
47 IcmpCode = 0;\r
48 IcmpPoint = 0;\r
49\r
50 Cur = 0;\r
51\r
52 while (Cur < OptionLen) {\r
53 switch (Option[Cur]) {\r
54 case IP4_OPTION_NOP:\r
55 Cur++;\r
56 break;\r
57\r
58 case IP4_OPTION_EOP:\r
59 Cur = OptionLen;\r
60 break;\r
61\r
62 case IP4_OPTION_LSRR:\r
63 case IP4_OPTION_SSRR:\r
64 case IP4_OPTION_RR:\r
65 Len = Option[Cur + 1];\r
66 Point = Option[Cur + 2];\r
67\r
68 //\r
96e1079f 69 // SRR/RR options are formatted as |Type|Len|Point|Ip1|Ip2|...\r
772db4bb 70 //\r
71 if ((OptionLen - Cur < Len) || (Len < 3) || ((Len - 3) % 4 != 0)) {\r
72 IcmpPoint = Cur + 1;\r
73 return FALSE;\r
74 }\r
75\r
76 if ((Point > Len + 1) || (Point % 4 != 0)) {\r
77 IcmpPoint = Cur + 2;\r
78 return FALSE;\r
79 }\r
80\r
81 //\r
82 // The Point must point pass the last entry if the packet is received\r
83 // by us. It must point to 4 if the packet is to be sent by us for\r
84 // source route option.\r
85 //\r
86 if ((Option[Cur] != IP4_OPTION_RR) &&\r
87 ((Rcvd && (Point != Len + 1)) || (!Rcvd && (Point != 4)))) {\r
88\r
89 IcmpType = ICMP_DEST_UNREACHABLE;\r
90 IcmpCode = ICMP_SOURCEROUTE_FAILED;\r
91 return FALSE;\r
92 }\r
93\r
94 Cur += Len;\r
95 break;\r
96\r
97 default:\r
98 Len = Option[Cur + 1];\r
99\r
100 if ((OptionLen - Cur < Len) || (Len < 2)) {\r
101 IcmpPoint = Cur + 1;\r
102 return FALSE;\r
103 }\r
104\r
105 Cur = Cur + Len;\r
106 break;\r
107 }\r
108\r
109 }\r
110\r
111 return TRUE;\r
112}\r
113\r
114\r
115/**\r
116 Copy the option from the original option to buffer. It\r
117 handles the details such as:\r
118 1. whether copy the single IP4 option to the first/non-first\r
96e1079f 119 fragments.\r
120 2. Pad the options copied over to aligned to 4 bytes.\r
772db4bb 121\r
3e8c18da 122 @param[in] Option The original option to copy from\r
123 @param[in] OptionLen The length of the original option\r
124 @param[in] FirstFragment Whether it is the first fragment\r
125 @param[in, out] Buf The buffer to copy options to. NULL \r
126 @param[in, out] BufLen The length of the buffer\r
772db4bb 127\r
128 @retval EFI_SUCCESS The options are copied over\r
96e1079f 129 @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small.\r
772db4bb 130\r
131**/\r
132EFI_STATUS\r
133Ip4CopyOption (\r
96e1079f 134 IN UINT8 *Option,\r
135 IN UINT32 OptionLen,\r
136 IN BOOLEAN FirstFragment,\r
137 IN OUT UINT8 *Buf, OPTIONAL\r
772db4bb 138 IN OUT UINT32 *BufLen\r
139 )\r
140{\r
141 UINT8 OptBuf[40];\r
142 UINT32 Cur;\r
143 UINT32 Next;\r
144 UINT8 Type;\r
145 UINT32 Len;\r
146\r
147 ASSERT ((BufLen != NULL) && (OptionLen <= 40));\r
148\r
149 Cur = 0;\r
150 Next = 0;\r
151\r
152 while (Cur < OptionLen) {\r
153 Type = Option[Cur];\r
154 Len = Option[Cur + 1];\r
155\r
156 if (Type == IP4_OPTION_NOP) {\r
157 //\r
158 // Keep the padding, in case that the sender wants to align\r
159 // the option, say, to 4 bytes\r
160 //\r
161 OptBuf[Next] = IP4_OPTION_NOP;\r
162 Next++;\r
163 Cur++;\r
164\r
165 } else if (Type == IP4_OPTION_EOP) {\r
166 //\r
167 // Don't append the EOP to avoid including only a EOP option\r
168 //\r
169 break;\r
170\r
171 } else {\r
172 //\r
173 // don't copy options that is only valid for the first fragment\r
174 //\r
5405e9a6 175 if (FirstFragment || (Type & IP4_OPTION_COPY_MASK) != 0) {\r
e48e37fc 176 CopyMem (OptBuf + Next, Option + Cur, Len);\r
772db4bb 177 Next += Len;\r
178 }\r
179\r
180 Cur += Len;\r
181 }\r
182 }\r
183\r
184 //\r
185 // Don't append an EOP only option.\r
186 //\r
187 if (Next == 0) {\r
188 *BufLen = 0;\r
189 return EFI_SUCCESS;\r
190 }\r
191\r
192 //\r
193 // Append an EOP if the end of option doesn't coincide with the\r
194 // end of the IP header, that is, isn't aligned to 4 bytes..\r
195 //\r
196 if ((Next % 4) != 0) {\r
197 OptBuf[Next] = IP4_OPTION_EOP;\r
198 Next++;\r
199 }\r
200\r
201 //\r
202 // Head length is in the unit of 4 bytes. Now, Len is the\r
203 // acutal option length to appear in the IP header.\r
204 //\r
205 Len = ((Next + 3) &~0x03);\r
206\r
207 //\r
208 // If the buffer is too small, set the BufLen then return\r
209 //\r
210 if ((Buf == NULL) || (*BufLen < Len)) {\r
211 *BufLen = Len;\r
212 return EFI_BUFFER_TOO_SMALL;\r
213 }\r
214\r
215 //\r
216 // Copy the option to the Buf, zero the buffer first to pad\r
217 // the options with NOP to align to 4 bytes.\r
218 //\r
e48e37fc 219 ZeroMem (Buf, Len);\r
220 CopyMem (Buf, OptBuf, Next);\r
772db4bb 221 *BufLen = Len;\r
222 return EFI_SUCCESS;\r
223}\r