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