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