summaryrefslogtreecommitdiffstats
path: root/src/net/dhcpopts.c
blob: faa3a50a756d51fe346100f8c3a8f0d784b42e83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <byteswap.h>
#include <errno.h>
#include <malloc.h>
#include <assert.h>
#include <gpxe/list.h>
#include <gpxe/dhcp.h>

/** @file
 *
 * DHCP options
 *
 */

/** List of registered DHCP option blocks */
static LIST_HEAD ( option_blocks );

/**
 * Obtain value of a numerical DHCP option
 *
 * @v option		DHCP option, or NULL
 * @ret value		Numerical value of the option, or 0
 *
 * Parses the numerical value from a DHCP option, if present.  It is
 * permitted to call dhcp_num_option() with @c option set to NULL; in
 * this case 0 will be returned.
 *
 * The caller does not specify the size of the DHCP option data; this
 * is implied by the length field stored within the DHCP option
 * itself.
 */
unsigned long dhcp_num_option ( struct dhcp_option *option ) {
	unsigned long value = 0;
	uint8_t *data;

	if ( option ) {
		/* This is actually smaller code than using htons()
		 * etc., and will also cope well with malformed
		 * options (such as zero-length options).
		 */
		for ( data = option->data.bytes ;
		      data < ( option->data.bytes + option->len ) ; data++ )
			value = ( ( value << 8 ) | *data );
	}
	return value;
}

/**
 * Calculate length of a DHCP option
 *
 * @v option		DHCP option
 * @ret len		Length (including tag and length field)
 */
static inline unsigned int dhcp_option_len ( struct dhcp_option *option ) {
	if ( ( option->tag == DHCP_END ) || ( option->tag == DHCP_PAD ) ) {
		return 1;
	} else {
		return ( option->len + 2 );
	}
}

/**
 * Find DHCP option within DHCP options block, and its encapsulator (if any)
 *
 * @v tag		DHCP option tag to search for
 * @v options		DHCP options block
 * @ret encapsulator	Encapsulating option (if applicable, may be NULL)
 * @ret option		DHCP option, or NULL if not found
 *
 * Searches for the DHCP option matching the specified tag within the
 * block of data.  Encapsulated options may be searched for by using
 * DHCP_ENCAP_OPT() to construct the tag value.  If the option is an
 * encapsulated option, and @c encapsulator is non-NULL, it will be
 * filled in with a pointer to the encapsulating option, if present.
 * Note that the encapsulating option may be present even if the
 * encapsulated option is absent, in which case @c encapsulator will
 * be set but the function will return NULL.
 *
 * This routine is designed to be paranoid.  It does not assume that
 * the option data is well-formatted, and so must guard against flaws
 * such as options missing a @c DHCP_END terminator, or options whose
 * length would take them beyond the end of the data block.
 */
static struct dhcp_option *
find_dhcp_option_encap ( unsigned int tag, struct dhcp_option_block *options,
			 struct dhcp_option **encapsulator ) {
	struct dhcp_option *option = options->data;
	ssize_t remaining = options->len;
	unsigned int option_len;

	while ( remaining ) {
		/* Calculate length of this option.  Abort processing
		 * if the length is malformed (i.e. takes us beyond
		 * the end of the data block).
		 */
		option_len = dhcp_option_len ( option );
		remaining -= option_len;
		if ( remaining < 0 )
			break;
		/* Check for matching tag */
		if ( option->tag == tag )
			return option;
		/* Check for explicit end marker */
		if ( option->tag == DHCP_END )
			break;
		/* Check for start of matching encapsulation block */
		if ( DHCP_ENCAPSULATOR ( tag ) &&
		     ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
			/* Continue search within encapsulated option block */
			if ( encapsulator )
				*encapsulator = option;
			remaining = option->len;
			option = ( void * ) &option->data;
			continue;
		}
		option = ( ( ( void * ) option ) + option_len );
	}
	return NULL;
}

/**
 * Find DHCP option within DHCP options block
 *
 * @v tag		DHCP option tag to search for
 * @v options		DHCP options block
 * @ret option		DHCP option, or NULL if not found
 *
 * Searches for the DHCP option matching the specified tag within the
 * block of data.  Encapsulated options may be searched for by using
 * DHCP_ENCAP_OPT() to construct the tag value.
 */
struct dhcp_option * find_dhcp_option ( unsigned int tag,
					struct dhcp_option_block *options ) {
	return find_dhcp_option_encap ( tag, options, NULL );
}

/**
 * Find length of used portion of DHCP options block
 *
 * @v options		DHCP options block
 * @ret len		Length of used portion of data block
 *
 * This searches for the @c DHCP_END marker within the options block.
 * If found, the length of the used portion of the block (i.e. the
 * portion containing everything @b before the @c DHCP_END marker, but
 * excluding the @c DHCP_END marker itself) is returned.
 *
 * If no @c DHCP_END marker is present, the length of the whole
 * options block is returned.
 */
size_t dhcp_option_block_len ( struct dhcp_option_block *options ) {
	void *dhcpend;

	if ( ( dhcpend = find_dhcp_option ( DHCP_END, options ) ) ) {
		return ( dhcpend - options->data );
	} else {
		return options->len;
	}
}

/**
 * Find DHCP option within all registered DHCP options blocks
 *
 * @v tag		DHCP option tag to search for
 * @ret option		DHCP option, or NULL if not found
 *
 * Searches within all registered DHCP option blocks for the specified
 * tag.  Encapsulated options may be searched for by using
 * DHCP_ENCAP_OPT() to construct the tag value.
 *
 * Where multiple option blocks contain the same DHCP option, the
 * option from the highest-priority block will be returned.  (Priority
 * of an options block is determined by the value of the @c
 * DHCP_EB_PRIORITY option within the block, if present; the default
 * priority is zero).
 */
struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
	struct dhcp_option_block *options;
	struct dhcp_option *option;

	list_for_each_entry ( options, &option_blocks, list ) {
		if ( ( option = find_dhcp_option ( tag, options ) ) )
			return option;
	}
	return NULL;
}

/**
 * Register DHCP option block
 *
 * @v options		DHCP option block
 *
 * Register a block of DHCP options.
 */
void register_dhcp_options ( struct dhcp_option_block *options ) {
	struct dhcp_option_block *existing_options;
	signed int existing_priority;
	signed int priority;

	/* Determine priority of new block */
	priority = find_dhcp_num_option ( DHCP_EB_PRIORITY, options );

	/* Insert after any existing blocks which have a higher priority */
	list_for_each_entry ( existing_options, &option_blocks, list ) {
		existing_priority = find_dhcp_num_option ( DHCP_EB_PRIORITY,
							   existing_options );
		if ( priority > existing_priority )
			break;
	}
	list_add_tail ( &options->list, &existing_options->list );
}

/**
 * Unregister DHCP option block
 *
 * @v options		DHCP option block
 */
void unregister_dhcp_options ( struct dhcp_option_block *options ) {
	list_del ( &options->list );
}

/**
 * Allocate space for a block of DHCP options
 *
 * @v len		Maximum length of option block
 * @ret options		Option block, or NULL
 *
 * Creates a new DHCP option block and populates it with an empty
 * options list.  This call does not register the options block.
 */
struct dhcp_option_block * alloc_dhcp_options ( size_t len ) {
	struct dhcp_option_block *options;
	struct dhcp_option *option;

	options = malloc ( sizeof ( *options ) + len );
	if ( options ) {
		options->data = ( ( void * ) options + sizeof ( *options ) );
		options->len = len;
		if ( len ) {
			option = options->data;
			option->tag = DHCP_END;
		}
	}
	return options;
}

/**
 * Free DHCP options block
 *
 * @v options		Option block
 */
void free_dhcp_options ( struct dhcp_option_block *options ) {
	free ( options );
}