|
@@ -93,10 +93,6 @@ static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
|
93
|
93
|
* the option data is well-formatted, and so must guard against flaws
|
94
|
94
|
* such as options missing a @c DHCP_END terminator, or options whose
|
95
|
95
|
* length would take them beyond the end of the data block.
|
96
|
|
- *
|
97
|
|
- * Searching for @c DHCP_PAD or @c DHCP_END tags, or using either @c
|
98
|
|
- * DHCP_PAD or @c DHCP_END as the encapsulator when constructing the
|
99
|
|
- * tag via DHCP_ENCAP_OPT() will produce undefined behaviour.
|
100
|
96
|
*/
|
101
|
97
|
static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
|
102
|
98
|
void *data, size_t len ) {
|
|
@@ -104,14 +100,7 @@ static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
|
104
|
100
|
ssize_t remaining = len;
|
105
|
101
|
unsigned int option_len;
|
106
|
102
|
|
107
|
|
- assert ( tag != DHCP_PAD );
|
108
|
|
- assert ( tag != DHCP_END );
|
109
|
|
- assert ( DHCP_ENCAPSULATOR ( tag ) != DHCP_END );
|
110
|
|
-
|
111
|
103
|
while ( remaining ) {
|
112
|
|
- /* Check for explicit end marker */
|
113
|
|
- if ( option->tag == DHCP_END )
|
114
|
|
- break;
|
115
|
104
|
/* Calculate length of this option. Abort processing
|
116
|
105
|
* if the length is malformed (i.e. takes us beyond
|
117
|
106
|
* the end of the data block).
|
|
@@ -123,6 +112,9 @@ static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
|
123
|
112
|
/* Check for matching tag */
|
124
|
113
|
if ( option->tag == tag )
|
125
|
114
|
return option;
|
|
115
|
+ /* Check for explicit end marker */
|
|
116
|
+ if ( option->tag == DHCP_END )
|
|
117
|
+ break;
|
126
|
118
|
/* Check for start of matching encapsulation block */
|
127
|
119
|
if ( DHCP_ENCAPSULATOR ( tag ) &&
|
128
|
120
|
( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
|
|
@@ -136,6 +128,46 @@ static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
|
136
|
128
|
return NULL;
|
137
|
129
|
}
|
138
|
130
|
|
|
131
|
+/**
|
|
132
|
+ * Find DHCP option within options block
|
|
133
|
+ *
|
|
134
|
+ * @v tag DHCP option tag to search for
|
|
135
|
+ * @v options DHCP options block
|
|
136
|
+ * @ret option DHCP option, or NULL if not found
|
|
137
|
+ *
|
|
138
|
+ * Searches for the DHCP option matching the specified tag within the
|
|
139
|
+ * options block. Encapsulated options may be searched for by using
|
|
140
|
+ * DHCP_ENCAP_OPT() to construct the tag value.
|
|
141
|
+ */
|
|
142
|
+struct dhcp_option * find_dhcp_option ( unsigned int tag,
|
|
143
|
+ struct dhcp_option_block *options ) {
|
|
144
|
+ return find_dhcp_option_raw ( tag, options->data, options->len );
|
|
145
|
+}
|
|
146
|
+
|
|
147
|
+/**
|
|
148
|
+ * Find length of used portion of DHCP options block
|
|
149
|
+ *
|
|
150
|
+ * @v options DHCP options block
|
|
151
|
+ * @ret len Length of used portion of data block
|
|
152
|
+ *
|
|
153
|
+ * This searches for the @c DHCP_END marker within the options block.
|
|
154
|
+ * If found, the length of the used portion of the block (i.e. the
|
|
155
|
+ * portion containing everything @b before the @c DHCP_END marker, but
|
|
156
|
+ * excluding the @c DHCP_END marker itself) is returned.
|
|
157
|
+ *
|
|
158
|
+ * If no @c DHCP_END marker is present, the length of the whole
|
|
159
|
+ * options block is returned.
|
|
160
|
+ */
|
|
161
|
+size_t dhcp_option_block_len ( struct dhcp_option_block *options ) {
|
|
162
|
+ void *dhcpend;
|
|
163
|
+
|
|
164
|
+ if ( ( dhcpend = find_dhcp_option ( DHCP_END, options ) ) ) {
|
|
165
|
+ return ( dhcpend - options->data );
|
|
166
|
+ } else {
|
|
167
|
+ return options->len;
|
|
168
|
+ }
|
|
169
|
+}
|
|
170
|
+
|
139
|
171
|
/**
|
140
|
172
|
* Find DHCP option within all registered DHCP options blocks
|
141
|
173
|
*
|
|
@@ -145,14 +177,19 @@ static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
|
145
|
177
|
* Searches within all registered DHCP option blocks for the specified
|
146
|
178
|
* tag. Encapsulated options may be searched for by using
|
147
|
179
|
* DHCP_ENCAP_OPT() to construct the tag value.
|
|
180
|
+ *
|
|
181
|
+ * Where multiple option blocks contain the same DHCP option, the
|
|
182
|
+ * option from the highest-priority block will be returned. (Priority
|
|
183
|
+ * of an options block is determined by the value of the @c
|
|
184
|
+ * DHCP_EB_PRIORITY option within the block, if present; the default
|
|
185
|
+ * priority is zero).
|
148
|
186
|
*/
|
149
|
|
-struct dhcp_option * find_dhcp_option ( unsigned int tag ) {
|
|
187
|
+struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
|
150
|
188
|
struct dhcp_option_block *options;
|
151
|
189
|
struct dhcp_option *option;
|
152
|
190
|
|
153
|
191
|
list_for_each_entry ( options, &option_blocks, list ) {
|
154
|
|
- if ( ( option = find_dhcp_option_raw ( tag, options->data,
|
155
|
|
- options->len ) ) )
|
|
192
|
+ if ( ( option = find_dhcp_option ( tag, options ) ) )
|
156
|
193
|
return option;
|
157
|
194
|
}
|
158
|
195
|
return NULL;
|
|
@@ -163,10 +200,24 @@ struct dhcp_option * find_dhcp_option ( unsigned int tag ) {
|
163
|
200
|
*
|
164
|
201
|
* @v options DHCP option block
|
165
|
202
|
*
|
166
|
|
- * Register a block of DHCP options
|
|
203
|
+ * Register a block of DHCP options.
|
167
|
204
|
*/
|
168
|
205
|
void register_dhcp_options ( struct dhcp_option_block *options ) {
|
169
|
|
- list_add ( &options->list, &option_blocks );
|
|
206
|
+ struct dhcp_option_block *existing_options;
|
|
207
|
+ signed int existing_priority;
|
|
208
|
+ signed int priority;
|
|
209
|
+
|
|
210
|
+ /* Determine priority of new block */
|
|
211
|
+ priority = find_dhcp_num_option ( DHCP_EB_PRIORITY, options );
|
|
212
|
+
|
|
213
|
+ /* Insert after any existing blocks which have a higher priority */
|
|
214
|
+ list_for_each_entry ( existing_options, &option_blocks, list ) {
|
|
215
|
+ existing_priority = find_dhcp_num_option ( DHCP_EB_PRIORITY,
|
|
216
|
+ existing_options );
|
|
217
|
+ if ( priority > existing_priority )
|
|
218
|
+ break;
|
|
219
|
+ }
|
|
220
|
+ list_add_tail ( &options->list, &existing_options->list );
|
170
|
221
|
}
|
171
|
222
|
|
172
|
223
|
/**
|