blob: 5bef985bc0b145f40bcf39aaae2b554c0d27866d [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
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010014/* Supported options are easily added here.
15 * See RFC2132 for more options.
Denys Vlasenko87fa2162010-03-20 18:06:23 +010016 * OPTION_REQ: these options are requested by udhcpc (unless -o).
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010017 */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000018const struct dhcp_option dhcp_options[] = {
Denis Vlasenko19183682007-12-10 07:03:38 +000019 /* flags code */
20 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
21 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
22 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010023// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010024// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000025 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010026// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010027// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000028 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
29 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
30 { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010031 { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000032 { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */
33 { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */
34 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
35 { OPTION_U16 , 0x1a }, /* DHCP_MTU */
36 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010037 { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */
38 { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000039 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
40 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000041 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
Denis Vlasenko19183682007-12-10 07:03:38 +000042 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010043 { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */
44//TODO: must be combined with 'sname' and 'file' handling:
45 { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */
46 { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010047//TODO: not a string, but a set of LASCII strings:
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010048// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */
Denis Vlasenko35a064b2008-11-06 00:49:59 +000049#if ENABLE_FEATURE_UDHCP_RFC3397
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010050 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */
Denis Vlasenko50664732007-02-27 21:15:08 +000051#endif
Denys Vlasenko97ef6542009-07-03 18:14:03 +020052 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010053 { OPTION_STRING , 0xfc }, /* DHCP_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
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010057 * with "option XXX YYY" syntax in dhcpd config file.
58 * These entries are only used internally by udhcp[cd]
59 * to correctly encode options into packets.
60 */
Denis Vlasenkob539c842007-11-29 08:17:45 +000061
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010062 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
63 { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */
Denis Vlasenko19183682007-12-10 07:03:38 +000064 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010065 { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */
66//FIXME: handling of this option is not exactly correct:
67 { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */
Denys Vlasenko043b1e52009-09-06 12:47:55 +020068 { 0, 0 } /* zeroed terminating entry */
Mike Frysinger7031f622006-05-08 03:20:50 +000069};
70
Denis Vlasenkob539c842007-11-29 08:17:45 +000071/* Used for converting options from incoming packets to env variables
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010072 * for udhcpc stript, and for setting options for udhcpd via
73 * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file.
74 */
Denis Vlasenkob539c842007-11-29 08:17:45 +000075/* Must match dhcp_options[] order */
76const char dhcp_option_strings[] ALIGN1 =
77 "subnet" "\0" /* DHCP_SUBNET */
78 "timezone" "\0" /* DHCP_TIME_OFFSET */
79 "router" "\0" /* DHCP_ROUTER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010080// "timesrv" "\0" /* DHCP_TIME_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010081// "namesrv" "\0" /* DHCP_NAME_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000082 "dns" "\0" /* DHCP_DNS_SERVER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010083// "logsrv" "\0" /* DHCP_LOG_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010084// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000085 "lprsrv" "\0" /* DHCP_LPR_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000086 "hostname" "\0" /* DHCP_HOST_NAME */
87 "bootsize" "\0" /* DHCP_BOOT_SIZE */
88 "domain" "\0" /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000089 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000090 "rootpath" "\0" /* DHCP_ROOT_PATH */
91 "ipttl" "\0" /* DHCP_IP_TTL */
92 "mtu" "\0" /* DHCP_MTU */
93 "broadcast" "\0" /* DHCP_BROADCAST */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010094 "nisdomain" "\0" /* DHCP_NIS_DOMAIN */
95 "nissrv" "\0" /* DHCP_NIS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000096 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
97 "wins" "\0" /* DHCP_WINS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000098 "lease" "\0" /* DHCP_LEASE_TIME */
Denis Vlasenko19183682007-12-10 07:03:38 +000099 "serverid" "\0" /* DHCP_SERVER_ID */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100100 "message" "\0" /* DHCP_ERR_MESSAGE */
101 "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */
102 "bootfile" "\0" /* DHCP_BOOT_FILE */
103// "userclass" "\0" /* DHCP_USER_CLASS */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000104#if ENABLE_FEATURE_UDHCP_RFC3397
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100105 "search" "\0" /* DHCP_DOMAIN_SEARCH */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000106#endif
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +0100107// "staticroutes" is only used to set udhcpc environment, it doesn't work
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100108// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet
109// by "string->option" conversion code:
110 "staticroutes" "\0"/* DHCP_STATIC_ROUTES */
111 "wpad" "\0" /* DHCP_WPAD */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000112 ;
113
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000114
Mike Frysinger7031f622006-05-08 03:20:50 +0000115/* Lengths of the different option types */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000116const uint8_t dhcp_option_lengths[] ALIGN1 = {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000117 [OPTION_IP] = 4,
118 [OPTION_IP_PAIR] = 8,
119 [OPTION_BOOLEAN] = 1,
120 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000121#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000122 [OPTION_STR1035] = 1,
123#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000124 [OPTION_U8] = 1,
125 [OPTION_U16] = 2,
126 [OPTION_S16] = 2,
127 [OPTION_U32] = 4,
Denys Vlasenko7895b912009-07-03 16:59:59 +0200128 [OPTION_S32] = 4,
Denys Vlasenko97ef6542009-07-03 18:14:03 +0200129 /* Just like OPTION_STRING, we use minimum length here */
130 [OPTION_STATIC_ROUTES] = 5,
Mike Frysinger7031f622006-05-08 03:20:50 +0000131};
132
133
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200134#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
135static void log_option(const char *pfx, const uint8_t *opt)
136{
137 if (dhcp_verbose >= 2) {
138 char buf[256 * 2 + 2];
139 *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0';
140 bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf);
141 }
142}
143#else
144# define log_option(pfx, opt) ((void)0)
145#endif
146
147
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000148/* get an option with bounds checking (warning, result is not aligned). */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200149uint8_t* FAST_FUNC get_option(struct dhcp_packet *packet, int code)
Mike Frysinger7031f622006-05-08 03:20:50 +0000150{
Mike Frysinger7031f622006-05-08 03:20:50 +0000151 uint8_t *optionptr;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000152 int len;
153 int rem;
154 int overload = 0;
155 enum {
156 FILE_FIELD101 = FILE_FIELD * 0x101,
157 SNAME_FIELD101 = SNAME_FIELD * 0x101,
158 };
Mike Frysinger7031f622006-05-08 03:20:50 +0000159
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000160 /* option bytes: [code][len][data1][data2]..[dataLEN] */
Mike Frysinger7031f622006-05-08 03:20:50 +0000161 optionptr = packet->options;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000162 rem = sizeof(packet->options);
Denis Vlasenko6884f662007-11-23 00:08:54 +0000163 while (1) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000164 if (rem <= 0) {
165 bb_error_msg("bogus packet, malformed option field");
Mike Frysinger7031f622006-05-08 03:20:50 +0000166 return NULL;
167 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000168 if (optionptr[OPT_CODE] == DHCP_PADDING) {
169 rem--;
170 optionptr++;
171 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000172 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000173 if (optionptr[OPT_CODE] == DHCP_END) {
174 if ((overload & FILE_FIELD101) == FILE_FIELD) {
175 /* can use packet->file, and didn't look at it yet */
176 overload |= FILE_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000177 optionptr = packet->file;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000178 rem = sizeof(packet->file);
179 continue;
180 }
181 if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
182 /* can use packet->sname, and didn't look at it yet */
183 overload |= SNAME_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 optionptr = packet->sname;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000185 rem = sizeof(packet->sname);
186 continue;
187 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200188 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000190 len = 2 + optionptr[OPT_LEN];
191 rem -= len;
192 if (rem < 0)
193 continue; /* complain and return NULL */
194
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200195 if (optionptr[OPT_CODE] == code) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200196 log_option("Option found", optionptr);
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000197 return optionptr + OPT_DATA;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200198 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000199
200 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
201 overload |= optionptr[OPT_DATA];
202 /* fall through */
203 }
204 optionptr += len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000205 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200206
207 /* log3 because udhcpc uses it a lot - very noisy */
208 log3("Option 0x%02x not found", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000209 return NULL;
210}
211
212
213/* return the position of the 'end' option (no bounds checking) */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000214int FAST_FUNC end_option(uint8_t *optionptr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000215{
216 int i = 0;
217
218 while (optionptr[i] != DHCP_END) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000219 if (optionptr[i] != DHCP_PADDING)
220 i += optionptr[i + OPT_LEN] + 1;
221 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000222 }
223 return i;
224}
225
226
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000227/* add an option string to the options */
228/* option bytes: [code][len][data1][data2]..[dataLEN] */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100229void FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
Mike Frysinger7031f622006-05-08 03:20:50 +0000230{
231 int end = end_option(optionptr);
232
233 /* end position + string length + option code/length + end option */
Denis Vlasenko72e76042007-11-25 03:15:24 +0000234 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000235 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000236 string[OPT_CODE]);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100237 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000238 }
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200239 log_option("Adding option", string);
Mike Frysinger7031f622006-05-08 03:20:50 +0000240 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
241 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
Mike Frysinger7031f622006-05-08 03:20:50 +0000242}
243
244
245/* add a one to four byte option to a packet */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100246void FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
Mike Frysinger7031f622006-05-08 03:20:50 +0000247{
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000248 const struct dhcp_option *dh;
Mike Frysinger7031f622006-05-08 03:20:50 +0000249
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000250 for (dh = dhcp_options; dh->code; dh++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000251 if (dh->code == code) {
252 uint8_t option[6], len;
253
254 option[OPT_CODE] = code;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000255 len = dhcp_option_lengths[dh->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000256 option[OPT_LEN] = len;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000257 if (BB_BIG_ENDIAN)
258 data <<= 8 * (4 - len);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000259 /* Assignment is unaligned! */
260 move_to_unaligned32(&option[OPT_DATA], data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100261 add_option_string(optionptr, option);
262 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000263 }
264 }
265
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100266 bb_error_msg("can't add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000267}