]>
Commit | Line | Data |
---|---|---|
83cbd279 | 1 | /** @file\r |
3e8c18da | 2 | To validate, parse and process the DHCP options.\r |
d1102dba LG |
3 | \r |
4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
e5eed7d3 | 5 | This program and the accompanying materials\r |
83cbd279 | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
83cbd279 | 13 | **/\r |
14 | \r | |
15 | #ifndef __EFI_DHCP4_OPTION_H__\r | |
16 | #define __EFI_DHCP4_OPTION_H__\r | |
17 | \r | |
f9204641 | 18 | ///\r |
19 | /// DHCP option tags (types)\r | |
20 | ///\r | |
f6b7393c | 21 | \r |
f6b7393c | 22 | #define DHCP_OPTION_MAGIC 0x63538263 // Network byte order\r |
23 | #define DHCP_MAX_OPTIONS 256\r | |
24 | \r | |
d1102dba | 25 | \r |
f6b7393c | 26 | //\r |
27 | // DHCP option types, this is used to validate the DHCP options.\r | |
28 | //\r | |
29 | #define DHCP_OPTION_SWITCH 1\r | |
30 | #define DHCP_OPTION_INT8 2\r | |
31 | #define DHCP_OPTION_INT16 3\r | |
32 | #define DHCP_OPTION_INT32 4\r | |
33 | #define DHCP_OPTION_IP 5\r | |
34 | #define DHCP_OPTION_IPPAIR 6\r | |
35 | \r | |
36 | //\r | |
37 | // Value of DHCP overload option\r | |
38 | //\r | |
39 | #define DHCP_OVERLOAD_FILENAME 1\r | |
40 | #define DHCP_OVERLOAD_SVRNAME 2\r | |
41 | #define DHCP_OVERLOAD_BOTH 3\r | |
83cbd279 | 42 | \r |
f9204641 | 43 | ///\r |
44 | /// The DHCP option structure. This structure extends the EFI_DHCP_OPTION\r | |
45 | /// structure to support options longer than 255 bytes, such as classless route.\r | |
46 | ///\r | |
83cbd279 | 47 | typedef struct {\r |
48 | UINT8 Tag;\r | |
49 | UINT16 Len;\r | |
50 | UINT8 *Data;\r | |
51 | } DHCP_OPTION;\r | |
52 | \r | |
f9204641 | 53 | ///\r |
54 | /// Structures used to parse the DHCP options with RFC3396 support.\r | |
55 | ///\r | |
83cbd279 | 56 | typedef struct {\r |
57 | UINT8 Index;\r | |
58 | UINT16 Offset;\r | |
59 | } DHCP_OPTION_COUNT;\r | |
60 | \r | |
61 | typedef struct {\r | |
62 | DHCP_OPTION_COUNT *OpCount;\r | |
63 | DHCP_OPTION *Options;\r | |
64 | UINT8 *Buf;\r | |
65 | } DHCP_OPTION_CONTEXT;\r | |
66 | \r | |
f9204641 | 67 | ///\r |
68 | /// The options that matters to DHCP driver itself. The user of\r | |
69 | /// DHCP clients may be interested in other options, such as\r | |
70 | /// classless route, who can parse the DHCP offer to get them.\r | |
71 | ///\r | |
83cbd279 | 72 | typedef struct {\r |
ac6c3d90 ZL |
73 | IP4_ADDR NetMask; // DHCP4_TAG_NETMASK\r |
74 | IP4_ADDR Router; // DHCP4_TAG_ROUTER, only the first router is used\r | |
83cbd279 | 75 | \r |
76 | //\r | |
77 | // DHCP specific options\r | |
78 | //\r | |
ac6c3d90 ZL |
79 | UINT8 DhcpType; // DHCP4_TAG_MSG_TYPE\r |
80 | UINT8 Overload; // DHCP4_TAG_OVERLOAD\r | |
81 | IP4_ADDR ServerId; // DHCP4_TAG_SERVER_ID\r | |
82 | UINT32 Lease; // DHCP4_TAG_LEASE\r | |
83 | UINT32 T1; // DHCP4_TAG_T1\r | |
84 | UINT32 T2; // DHCP4_TAG_T2\r | |
83cbd279 | 85 | } DHCP_PARAMETER;\r |
86 | \r | |
f9204641 | 87 | ///\r |
88 | /// Structure used to describe and validate the format of DHCP options.\r | |
89 | /// Type is the options' data type, such as DHCP_OPTION_INT8. MinOccur\r | |
90 | /// is the minium occurance of this data type. MaxOccur is defined\r | |
91 | /// similarly. If MaxOccur is -1, it means that there is no limit on the\r | |
92 | /// maximum occurance. Alert tells whether DHCP client should further\r | |
93 | /// inspect the option to parse DHCP_PARAMETER.\r | |
94 | ///\r | |
83cbd279 | 95 | typedef struct {\r |
96 | UINT8 Tag;\r | |
97 | INTN Type;\r | |
98 | INTN MinOccur;\r | |
99 | INTN MaxOccur;\r | |
100 | BOOLEAN Alert;\r | |
101 | } DHCP_OPTION_FORMAT;\r | |
102 | \r | |
103 | typedef\r | |
104 | EFI_STATUS\r | |
105 | (*DHCP_CHECK_OPTION) (\r | |
106 | IN UINT8 Tag,\r | |
107 | IN UINT8 Len,\r | |
108 | IN UINT8 *Data,\r | |
109 | IN VOID *Context\r | |
110 | );\r | |
111 | \r | |
7bce0c5a | 112 | /**\r |
113 | Iterate through a DHCP message to visit each option. First inspect\r | |
114 | all the options in the OPTION field. Then if overloaded, inspect\r | |
115 | the options in FILENAME and SERVERNAME fields. One option may be\r | |
116 | encoded in several places. See RFC 3396 Encoding Long Options in DHCP\r | |
117 | \r | |
3e8c18da | 118 | @param[in] Packet The DHCP packet to check the options for\r |
119 | @param[in] Check The callback function to be called for each option\r | |
120 | found\r | |
121 | @param[in] Context The opaque parameter for Check\r | |
7bce0c5a | 122 | \r |
123 | @retval EFI_SUCCESS The DHCP packet's options are well formated\r | |
ed729a0c | 124 | @retval EFI_INVALID_PARAMETER The DHCP packet's options are not well formated\r |
7bce0c5a | 125 | \r |
126 | **/\r | |
83cbd279 | 127 | EFI_STATUS\r |
128 | DhcpIterateOptions (\r | |
129 | IN EFI_DHCP4_PACKET *Packet,\r | |
24af132f | 130 | IN DHCP_CHECK_OPTION Check OPTIONAL,\r |
83cbd279 | 131 | IN VOID *Context\r |
132 | );\r | |
133 | \r | |
7bce0c5a | 134 | /**\r |
135 | Validate the packet's options. If necessary, allocate\r | |
136 | and fill in the interested parameters.\r | |
137 | \r | |
3e8c18da | 138 | @param[in] Packet The packet to validate the options\r |
139 | @param[out] Para The variable to save the DHCP parameters.\r | |
7bce0c5a | 140 | \r |
141 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to validate the packet.\r | |
142 | @retval EFI_INVALID_PARAMETER The options are mal-formated\r | |
143 | @retval EFI_SUCCESS The options are parsed into OptionPoint\r | |
144 | \r | |
145 | **/\r | |
83cbd279 | 146 | EFI_STATUS\r |
147 | DhcpValidateOptions (\r | |
148 | IN EFI_DHCP4_PACKET *Packet,\r | |
ed729a0c | 149 | OUT DHCP_PARAMETER **Para OPTIONAL\r |
83cbd279 | 150 | );\r |
151 | \r | |
7bce0c5a | 152 | /**\r |
153 | Parse the options of a DHCP packet. It supports RFC 3396: Encoding\r | |
154 | Long Options in DHCP. That is, it will combine all the option value\r | |
155 | of all the occurances of each option.\r | |
156 | A little bit of implemenation:\r | |
157 | It adopts the "Key indexed counting" algorithm. First, it allocates\r | |
158 | an array of 256 DHCP_OPTION_COUNTs because DHCP option tag is encoded\r | |
159 | as a UINT8. It then iterates the DHCP packet to get data length of\r | |
160 | each option by calling DhcpIterOptions with DhcpGetOptionLen. Now, it\r | |
161 | knows the number of present options and their length. It allocates a\r | |
ed729a0c | 162 | array of DHCP_OPTION and a continuous buffer after the array to put\r |
7bce0c5a | 163 | all the options' data. Each option's data is pointed to by the Data\r |
164 | field in DHCP_OPTION structure. At last, it call DhcpIterateOptions\r | |
165 | with DhcpFillOption to fill each option's data to its position in the\r | |
166 | buffer.\r | |
167 | \r | |
3e8c18da | 168 | @param[in] Packet The DHCP packet to parse the options\r |
169 | @param[out] Count The number of valid dhcp options present in the\r | |
170 | packet\r | |
171 | @param[out] OptionPoint The array that contains the DHCP options. Caller\r | |
172 | should free it.\r | |
7bce0c5a | 173 | \r |
7b0ae7e8 | 174 | @retval EFI_NOT_FOUND Cannot find any option.\r |
7bce0c5a | 175 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory to parse the packet.\r |
176 | @retval EFI_INVALID_PARAMETER The options are mal-formated\r | |
177 | @retval EFI_SUCCESS The options are parsed into OptionPoint\r | |
178 | \r | |
179 | **/\r | |
83cbd279 | 180 | EFI_STATUS\r |
181 | DhcpParseOption (\r | |
182 | IN EFI_DHCP4_PACKET *Packet,\r | |
183 | OUT INTN *Count,\r | |
184 | OUT DHCP_OPTION **OptionPoint\r | |
185 | );\r | |
186 | \r | |
7bce0c5a | 187 | /**\r |
188 | Append an option to the memory, if the option is longer than\r | |
189 | 255 bytes, splits it into several options.\r | |
190 | \r | |
3e8c18da | 191 | @param[out] Buf The buffer to append the option to\r |
192 | @param[in] Tag The option's tag\r | |
193 | @param[in] DataLen The length of the option's data\r | |
194 | @param[in] Data The option's data\r | |
7bce0c5a | 195 | \r |
196 | @return The position to append the next option\r | |
197 | \r | |
198 | **/\r | |
83cbd279 | 199 | UINT8 *\r |
200 | DhcpAppendOption (\r | |
ed729a0c | 201 | OUT UINT8 *Buf,\r |
202 | IN UINT8 Tag,\r | |
203 | IN UINT16 DataLen,\r | |
204 | IN UINT8 *Data\r | |
83cbd279 | 205 | );\r |
206 | \r | |
7bce0c5a | 207 | /**\r |
208 | Build a new DHCP packet from a seed packet. Options may be deleted or\r | |
209 | appended. The caller should free the NewPacket when finished using it.\r | |
210 | \r | |
3e8c18da | 211 | @param[in] SeedPacket The seed packet to start with\r |
212 | @param[in] DeleteCount The number of options to delete\r | |
213 | @param[in] DeleteList The options to delete from the packet\r | |
214 | @param[in] AppendCount The number of options to append\r | |
215 | @param[in] AppendList The options to append to the packet\r | |
216 | @param[out] NewPacket The new packet, allocated and built by this\r | |
217 | function.\r | |
7bce0c5a | 218 | \r |
219 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory\r | |
ed729a0c | 220 | @retval EFI_INVALID_PARAMETER The options in SeekPacket are mal-formated\r |
7bce0c5a | 221 | @retval EFI_SUCCESS The packet is build.\r |
222 | \r | |
223 | **/\r | |
83cbd279 | 224 | EFI_STATUS\r |
225 | DhcpBuild (\r | |
ed729a0c | 226 | IN EFI_DHCP4_PACKET *SeedPacket,\r |
227 | IN UINT32 DeleteCount,\r | |
228 | IN UINT8 *DeleteList OPTIONAL,\r | |
229 | IN UINT32 AppendCount,\r | |
230 | IN EFI_DHCP4_PACKET_OPTION *AppendList[] OPTIONAL,\r | |
231 | OUT EFI_DHCP4_PACKET **NewPacket\r | |
83cbd279 | 232 | );\r |
233 | \r | |
234 | #endif\r |