blob: b4d2d2de302eaf6bd3317c5ea523bd5ce2e54031 [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"
Mike Frysinger7031f622006-05-08 03:20:50 +000010
11
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010012/* Supported options are easily added here.
13 * See RFC2132 for more options.
Denys Vlasenko87fa2162010-03-20 18:06:23 +010014 * OPTION_REQ: these options are requested by udhcpc (unless -o).
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010015 */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000016const struct dhcp_option dhcp_options[] = {
Denis Vlasenko19183682007-12-10 07:03:38 +000017 /* flags code */
18 { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
19 { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
20 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010021// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010022// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000023 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010024// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010025// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000026 { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
27 { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
28 { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010029 { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000030 { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */
31 { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */
32 { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
33 { OPTION_U16 , 0x1a }, /* DHCP_MTU */
34 { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010035 { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */
36 { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000037 { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
38 { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000039 { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
Denis Vlasenko19183682007-12-10 07:03:38 +000040 { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010041 { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */
42//TODO: must be combined with 'sname' and 'file' handling:
43 { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */
44 { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010045//TODO: not a string, but a set of LASCII strings:
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010046// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */
Denis Vlasenko35a064b2008-11-06 00:49:59 +000047#if ENABLE_FEATURE_UDHCP_RFC3397
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010048 { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */
Denis Vlasenko50664732007-02-27 21:15:08 +000049#endif
Denys Vlasenko97ef6542009-07-03 18:14:03 +020050 { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010051 { OPTION_STRING , 0xfc }, /* DHCP_WPAD */
Denis Vlasenkob539c842007-11-29 08:17:45 +000052
53 /* Options below have no match in dhcp_option_strings[],
54 * are not passed to dhcpc scripts, and cannot be specified
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010055 * with "option XXX YYY" syntax in dhcpd config file.
56 * These entries are only used internally by udhcp[cd]
57 * to correctly encode options into packets.
58 */
Denis Vlasenkob539c842007-11-29 08:17:45 +000059
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010060 { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
61 { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */
Denis Vlasenko19183682007-12-10 07:03:38 +000062 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010063 { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */
64//FIXME: handling of this option is not exactly correct:
65 { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */
Denys Vlasenko043b1e52009-09-06 12:47:55 +020066 { 0, 0 } /* zeroed terminating entry */
Mike Frysinger7031f622006-05-08 03:20:50 +000067};
68
Denis Vlasenkob539c842007-11-29 08:17:45 +000069/* Used for converting options from incoming packets to env variables
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +010070 * for udhcpc stript, and for setting options for udhcpd via
71 * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file.
72 */
Denis Vlasenkob539c842007-11-29 08:17:45 +000073/* Must match dhcp_options[] order */
74const char dhcp_option_strings[] ALIGN1 =
75 "subnet" "\0" /* DHCP_SUBNET */
76 "timezone" "\0" /* DHCP_TIME_OFFSET */
77 "router" "\0" /* DHCP_ROUTER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010078// "timesrv" "\0" /* DHCP_TIME_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010079// "namesrv" "\0" /* DHCP_NAME_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000080 "dns" "\0" /* DHCP_DNS_SERVER */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010081// "logsrv" "\0" /* DHCP_LOG_SERVER */
Denys Vlasenko777706c2010-03-19 23:42:23 +010082// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000083 "lprsrv" "\0" /* DHCP_LPR_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000084 "hostname" "\0" /* DHCP_HOST_NAME */
85 "bootsize" "\0" /* DHCP_BOOT_SIZE */
86 "domain" "\0" /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000087 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000088 "rootpath" "\0" /* DHCP_ROOT_PATH */
89 "ipttl" "\0" /* DHCP_IP_TTL */
90 "mtu" "\0" /* DHCP_MTU */
91 "broadcast" "\0" /* DHCP_BROADCAST */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010092 "nisdomain" "\0" /* DHCP_NIS_DOMAIN */
93 "nissrv" "\0" /* DHCP_NIS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000094 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
95 "wins" "\0" /* DHCP_WINS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000096 "lease" "\0" /* DHCP_LEASE_TIME */
Denis Vlasenko19183682007-12-10 07:03:38 +000097 "serverid" "\0" /* DHCP_SERVER_ID */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010098 "message" "\0" /* DHCP_ERR_MESSAGE */
99 "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */
100 "bootfile" "\0" /* DHCP_BOOT_FILE */
101// "userclass" "\0" /* DHCP_USER_CLASS */
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000102#if ENABLE_FEATURE_UDHCP_RFC3397
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100103 "search" "\0" /* DHCP_DOMAIN_SEARCH */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000104#endif
Denys Vlasenko0f62c4d2010-03-20 07:12:21 +0100105// "staticroutes" is only used to set udhcpc environment, it doesn't work
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100106// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet
107// by "string->option" conversion code:
108 "staticroutes" "\0"/* DHCP_STATIC_ROUTES */
109 "wpad" "\0" /* DHCP_WPAD */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000110 ;
111
Mike Frysinger7031f622006-05-08 03:20:50 +0000112/* Lengths of the different option types */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000113const uint8_t dhcp_option_lengths[] ALIGN1 = {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000114 [OPTION_IP] = 4,
115 [OPTION_IP_PAIR] = 8,
116 [OPTION_BOOLEAN] = 1,
117 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000118#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000119 [OPTION_STR1035] = 1,
120#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000121 [OPTION_U8] = 1,
122 [OPTION_U16] = 2,
123 [OPTION_S16] = 2,
124 [OPTION_U32] = 4,
Denys Vlasenko7895b912009-07-03 16:59:59 +0200125 [OPTION_S32] = 4,
Denys Vlasenko97ef6542009-07-03 18:14:03 +0200126 /* Just like OPTION_STRING, we use minimum length here */
127 [OPTION_STATIC_ROUTES] = 5,
Mike Frysinger7031f622006-05-08 03:20:50 +0000128};
129
130
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200131#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
132static void log_option(const char *pfx, const uint8_t *opt)
133{
134 if (dhcp_verbose >= 2) {
135 char buf[256 * 2 + 2];
136 *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0';
137 bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf);
138 }
139}
140#else
141# define log_option(pfx, opt) ((void)0)
142#endif
143
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000144/* get an option with bounds checking (warning, result is not aligned). */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100145uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code)
Mike Frysinger7031f622006-05-08 03:20:50 +0000146{
Mike Frysinger7031f622006-05-08 03:20:50 +0000147 uint8_t *optionptr;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000148 int len;
149 int rem;
150 int overload = 0;
151 enum {
152 FILE_FIELD101 = FILE_FIELD * 0x101,
153 SNAME_FIELD101 = SNAME_FIELD * 0x101,
154 };
Mike Frysinger7031f622006-05-08 03:20:50 +0000155
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000156 /* option bytes: [code][len][data1][data2]..[dataLEN] */
Mike Frysinger7031f622006-05-08 03:20:50 +0000157 optionptr = packet->options;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000158 rem = sizeof(packet->options);
Denis Vlasenko6884f662007-11-23 00:08:54 +0000159 while (1) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000160 if (rem <= 0) {
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100161 bb_error_msg("bad packet, malformed option field");
Mike Frysinger7031f622006-05-08 03:20:50 +0000162 return NULL;
163 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000164 if (optionptr[OPT_CODE] == DHCP_PADDING) {
165 rem--;
166 optionptr++;
167 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000168 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000169 if (optionptr[OPT_CODE] == DHCP_END) {
170 if ((overload & FILE_FIELD101) == FILE_FIELD) {
171 /* can use packet->file, and didn't look at it yet */
172 overload |= FILE_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 optionptr = packet->file;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000174 rem = sizeof(packet->file);
175 continue;
176 }
177 if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
178 /* can use packet->sname, and didn't look at it yet */
179 overload |= SNAME_FIELD101; /* "we looked at it" */
Mike Frysinger7031f622006-05-08 03:20:50 +0000180 optionptr = packet->sname;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000181 rem = sizeof(packet->sname);
182 continue;
183 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200184 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000185 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000186 len = 2 + optionptr[OPT_LEN];
187 rem -= len;
188 if (rem < 0)
189 continue; /* complain and return NULL */
190
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200191 if (optionptr[OPT_CODE] == code) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200192 log_option("Option found", optionptr);
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000193 return optionptr + OPT_DATA;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200194 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000195
196 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
197 overload |= optionptr[OPT_DATA];
198 /* fall through */
199 }
200 optionptr += len;
Mike Frysinger7031f622006-05-08 03:20:50 +0000201 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200202
203 /* log3 because udhcpc uses it a lot - very noisy */
204 log3("Option 0x%02x not found", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000205 return NULL;
206}
207
Mike Frysinger7031f622006-05-08 03:20:50 +0000208/* return the position of the 'end' option (no bounds checking) */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100209int FAST_FUNC udhcp_end_option(uint8_t *optionptr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000210{
211 int i = 0;
212
213 while (optionptr[i] != DHCP_END) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000214 if (optionptr[i] != DHCP_PADDING)
215 i += optionptr[i + OPT_LEN] + 1;
216 i++;
Mike Frysinger7031f622006-05-08 03:20:50 +0000217 }
218 return i;
219}
220
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000221/* add an option string to the options */
222/* option bytes: [code][len][data1][data2]..[dataLEN] */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100223void FAST_FUNC udhcp_add_option_string(uint8_t *optionptr, uint8_t *string)
Mike Frysinger7031f622006-05-08 03:20:50 +0000224{
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100225 int end = udhcp_end_option(optionptr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000226
227 /* end position + string length + option code/length + end option */
Denis Vlasenko72e76042007-11-25 03:15:24 +0000228 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000229 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000230 string[OPT_CODE]);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100231 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000232 }
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200233 log_option("Adding option", string);
Mike Frysinger7031f622006-05-08 03:20:50 +0000234 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
235 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
Mike Frysinger7031f622006-05-08 03:20:50 +0000236}
237
Mike Frysinger7031f622006-05-08 03:20:50 +0000238/* add a one to four byte option to a packet */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100239void FAST_FUNC udhcp_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;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100248 len = dhcp_option_lengths[dh->flags & OPTION_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);
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100254 udhcp_add_option_string(optionptr, option);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100255 return;
Mike Frysinger7031f622006-05-08 03:20:50 +0000256 }
257 }
258
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100259 bb_error_msg("can't add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000260}