blob: 38f99196eed06f05178e286c55084ef7f0f330d7 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * options.c -- DHCP server option packet tools
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00007 */
8
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include "common.h"
10#include "dhcpd.h"
11#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000012
13
Denis Vlasenko35ff7462007-11-28 19:23:12 +000014/* Supported options are easily added here */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000015const struct dhcp_option dhcp_options[] = {
Denis Vlasenko19183682007-12-10 07:03:38 +000016 /* flags code */
17 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
18 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
19 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
20 { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
21 { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
22 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
23 { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
24 { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
25 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
26 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
27 { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */
28 { OPTION_STRING | OPTION_LIST | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */
29 { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */
30 { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */
31 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
32 { OPTION_U16 , 0x1a }, /* DHCP_MTU */
33 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
34 { OPTION_STRING , 0x28 }, /* nisdomain */
35 { OPTION_IP | OPTION_LIST , 0x29 }, /* nissrv */
36 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
37 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
38 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
39 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
40 { OPTION_U8 , 0x35 }, /* dhcptype */
41 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
42 { OPTION_STRING , 0x38 }, /* DHCP_MESSAGE */
43 { OPTION_STRING , 0x3C }, /* DHCP_VENDOR */
44 { OPTION_STRING , 0x3D }, /* DHCP_CLIENT_ID */
45 { OPTION_STRING , 0x42 }, /* tftp */
46 { OPTION_STRING , 0x43 }, /* bootfile */
47 { OPTION_STRING , 0x4D }, /* userclass */
Denis Vlasenko35a064b2008-11-06 00:49:59 +000048#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko19183682007-12-10 07:03:38 +000049 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* search */
Denis Vlasenko50664732007-02-27 21:15:08 +000050#endif
Denys Vlasenko97ef6542009-07-03 18:14:03 +020051 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000052 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
Denis Vlasenko19183682007-12-10 07:03:38 +000053 { OPTION_STRING , 0xfc }, /* wpad */
Denis Vlasenkob539c842007-11-29 08:17:45 +000054
55 /* Options below have no match in dhcp_option_strings[],
56 * are not passed to dhcpc scripts, and cannot be specified
57 * with "option XXX YYY" syntax in dhcpd config file. */
58
Denis Vlasenko19183682007-12-10 07:03:38 +000059 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
Denys Vlasenko043b1e52009-09-06 12:47:55 +020060 { 0, 0 } /* zeroed terminating entry */
Mike Frysinger7031f622006-05-08 03:20:50 +000061};
62
Denis Vlasenkob539c842007-11-29 08:17:45 +000063/* Used for converting options from incoming packets to env variables
64 * for udhcpc stript */
65/* Must match dhcp_options[] order */
66const char dhcp_option_strings[] ALIGN1 =
67 "subnet" "\0" /* DHCP_SUBNET */
68 "timezone" "\0" /* DHCP_TIME_OFFSET */
69 "router" "\0" /* DHCP_ROUTER */
Denis Vlasenko19183682007-12-10 07:03:38 +000070 "timesrv" "\0" /* DHCP_TIME_SERVER */
71 "namesrv" "\0" /* DHCP_NAME_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000072 "dns" "\0" /* DHCP_DNS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000073 "logsrv" "\0" /* DHCP_LOG_SERVER */
74 "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
75 "lprsrv" "\0" /* DHCP_LPR_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000076 "hostname" "\0" /* DHCP_HOST_NAME */
77 "bootsize" "\0" /* DHCP_BOOT_SIZE */
78 "domain" "\0" /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000079 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000080 "rootpath" "\0" /* DHCP_ROOT_PATH */
81 "ipttl" "\0" /* DHCP_IP_TTL */
82 "mtu" "\0" /* DHCP_MTU */
83 "broadcast" "\0" /* DHCP_BROADCAST */
Denis Vlasenko19183682007-12-10 07:03:38 +000084 "nisdomain" "\0" /* */
85 "nissrv" "\0" /* */
86 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
87 "wins" "\0" /* DHCP_WINS_SERVER */
88 "requestip" "\0" /* DHCP_REQUESTED_IP */
89 "lease" "\0" /* DHCP_LEASE_TIME */
90 "dhcptype" "\0" /* */
91 "serverid" "\0" /* DHCP_SERVER_ID */
Denis Vlasenkob539c842007-11-29 08:17:45 +000092 "message" "\0" /* DHCP_MESSAGE */
93 "vendorclass" "\0" /* DHCP_VENDOR */
94 "clientid" "\0" /* DHCP_CLIENT_ID */
95 "tftp" "\0"
96 "bootfile" "\0"
97 "userclass" "\0"
Denis Vlasenko35a064b2008-11-06 00:49:59 +000098#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenkob539c842007-11-29 08:17:45 +000099 "search" "\0"
100#endif
Denys Vlasenko97ef6542009-07-03 18:14:03 +0200101 "staticroutes" "\0" /* DHCP_STATIC_ROUTES */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000102 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
103 "wpad" "\0"
104 ;
105
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000106
Mike Frysinger7031f622006-05-08 03:20:50 +0000107/* Lengths of the different option types */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000108const uint8_t dhcp_option_lengths[] ALIGN1 = {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000109 [OPTION_IP] = 4,
110 [OPTION_IP_PAIR] = 8,
111 [OPTION_BOOLEAN] = 1,
112 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000113#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000114 [OPTION_STR1035] = 1,
115#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000116 [OPTION_U8] = 1,
117 [OPTION_U16] = 2,
118 [OPTION_S16] = 2,
119 [OPTION_U32] = 4,
Denys Vlasenko7895b912009-07-03 16:59:59 +0200120 [OPTION_S32] = 4,
Denys Vlasenko97ef6542009-07-03 18:14:03 +0200121 /* Just like OPTION_STRING, we use minimum length here */
122 [OPTION_STATIC_ROUTES] = 5,
Mike Frysinger7031f622006-05-08 03:20:50 +0000123};
124
125
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200126#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
127static void log_option(const char *pfx, const uint8_t *opt)
128{
129 if (dhcp_verbose >= 2) {
130 char buf[256 * 2 + 2];
131 *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0';
132 bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf);
133 }
134}
135#else
136# define log_option(pfx, opt) ((void)0)
137#endif
138
139
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000140/* get an option with bounds checking (warning, result is not aligned). */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200141uint8_t* FAST_FUNC get_option(struct dhcp_packet *packet, int code)
Mike Frysinger7031f622006-05-08 03:20:50 +0000142{
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 uint8_t *optionptr;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000144 int len;
145 int rem;
146 int overload = 0;
147 enum {
148 FILE_FIELD101 = FILE_FIELD * 0x101,
149 SNAME_FIELD101 = SNAME_FIELD * 0x101,
150 };
Mike Frysinger7031f622006-05-08 03:20:50 +0000151
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000152 /* option bytes: [code][len][data1][data2]..[dataLEN] */
Mike Frysinger7031f622006-05-08 03:20:50 +0000153 optionptr = packet->options;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000154 rem = sizeof(packet->options);
Denis Vlasenko6884f662007-11-23 00:08:54 +0000155 while (1) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000156 if (rem <= 0) {
157 bb_error_msg("bogus packet, malformed option field");
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 return NULL;
159 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000160 if (optionptr[OPT_CODE] == DHCP_PADDING) {
161 rem--;
162 optionptr++;
163 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000165 if (optionptr[OPT_CODE] == DHCP_END) {
166 if ((overload & FILE_FIELD101) == FILE_FIELD) {
167 /* can use packet->file, and didn't look at it yet */
168 overload |= FILE_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 optionptr = packet->file;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000170 rem = sizeof(packet->file);
171 continue;
172 }
173 if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
174 /* can use packet->sname, and didn't look at it yet */
175 overload |= SNAME_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000176 optionptr = packet->sname;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000177 rem = sizeof(packet->sname);
178 continue;
179 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200180 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000181 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000182 len = 2 + optionptr[OPT_LEN];
183 rem -= len;
184 if (rem < 0)
185 continue; /* complain and return NULL */
186
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200187 if (optionptr[OPT_CODE] == code) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200188 log_option("Option found", optionptr);
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000189 return optionptr + OPT_DATA;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200190 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000191
192 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
193 overload |= optionptr[OPT_DATA];
194 /* fall through */
195 }
196 optionptr += len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000197 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200198
199 /* log3 because udhcpc uses it a lot - very noisy */
200 log3("Option 0x%02x not found", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000201 return NULL;
202}
203
204
205/* return the position of the 'end' option (no bounds checking) */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000206int FAST_FUNC end_option(uint8_t *optionptr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000207{
208 int i = 0;
209
210 while (optionptr[i] != DHCP_END) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000211 if (optionptr[i] != DHCP_PADDING)
212 i += optionptr[i + OPT_LEN] + 1;
213 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000214 }
215 return i;
216}
217
218
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000219/* add an option string to the options */
220/* option bytes: [code][len][data1][data2]..[dataLEN] */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000221int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
Mike Frysinger7031f622006-05-08 03:20:50 +0000222{
223 int end = end_option(optionptr);
224
225 /* end position + string length + option code/length + end option */
Denis Vlasenko72e76042007-11-25 03:15:24 +0000226 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000227 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000228 string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000229 return 0;
230 }
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200231 log_option("Adding option", string);
Mike Frysinger7031f622006-05-08 03:20:50 +0000232 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
233 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
234 return string[OPT_LEN] + 2;
235}
236
237
238/* add a one to four byte option to a packet */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000239int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
Mike Frysinger7031f622006-05-08 03:20:50 +0000240{
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000241 const struct dhcp_option *dh;
Mike Frysinger7031f622006-05-08 03:20:50 +0000242
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000243 for (dh = dhcp_options; dh->code; dh++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000244 if (dh->code == code) {
245 uint8_t option[6], len;
246
247 option[OPT_CODE] = code;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000248 len = dhcp_option_lengths[dh->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000249 option[OPT_LEN] = len;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000250 if (BB_BIG_ENDIAN)
251 data <<= 8 * (4 - len);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000252 /* Assignment is unaligned! */
253 move_to_unaligned32(&option[OPT_DATA], data);
Mike Frysinger7031f622006-05-08 03:20:50 +0000254 return add_option_string(optionptr, option);
255 }
256 }
257
Denis Vlasenkoea620772006-10-14 02:23:43 +0000258 bb_error_msg("cannot add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000259 return 0;
260}