blob: 581a7b67134f7efcafd2a21c249d32921bc269d0 [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
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000051 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
Denis Vlasenko19183682007-12-10 07:03:38 +000052 { OPTION_STRING , 0xfc }, /* wpad */
Denis Vlasenkob539c842007-11-29 08:17:45 +000053
54 /* Options below have no match in dhcp_option_strings[],
55 * are not passed to dhcpc scripts, and cannot be specified
56 * with "option XXX YYY" syntax in dhcpd config file. */
57
Denis Vlasenko19183682007-12-10 07:03:38 +000058 { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
Denis Vlasenkob539c842007-11-29 08:17:45 +000059 { } /* zeroed terminating entry */
Mike Frysinger7031f622006-05-08 03:20:50 +000060};
61
Denis Vlasenkob539c842007-11-29 08:17:45 +000062/* Used for converting options from incoming packets to env variables
63 * for udhcpc stript */
64/* Must match dhcp_options[] order */
65const char dhcp_option_strings[] ALIGN1 =
66 "subnet" "\0" /* DHCP_SUBNET */
67 "timezone" "\0" /* DHCP_TIME_OFFSET */
68 "router" "\0" /* DHCP_ROUTER */
Denis Vlasenko19183682007-12-10 07:03:38 +000069 "timesrv" "\0" /* DHCP_TIME_SERVER */
70 "namesrv" "\0" /* DHCP_NAME_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000071 "dns" "\0" /* DHCP_DNS_SERVER */
Denis Vlasenko19183682007-12-10 07:03:38 +000072 "logsrv" "\0" /* DHCP_LOG_SERVER */
73 "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
74 "lprsrv" "\0" /* DHCP_LPR_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000075 "hostname" "\0" /* DHCP_HOST_NAME */
76 "bootsize" "\0" /* DHCP_BOOT_SIZE */
77 "domain" "\0" /* DHCP_DOMAIN_NAME */
Denis Vlasenko19183682007-12-10 07:03:38 +000078 "swapsrv" "\0" /* DHCP_SWAP_SERVER */
Denis Vlasenkob539c842007-11-29 08:17:45 +000079 "rootpath" "\0" /* DHCP_ROOT_PATH */
80 "ipttl" "\0" /* DHCP_IP_TTL */
81 "mtu" "\0" /* DHCP_MTU */
82 "broadcast" "\0" /* DHCP_BROADCAST */
Denis Vlasenko19183682007-12-10 07:03:38 +000083 "nisdomain" "\0" /* */
84 "nissrv" "\0" /* */
85 "ntpsrv" "\0" /* DHCP_NTP_SERVER */
86 "wins" "\0" /* DHCP_WINS_SERVER */
87 "requestip" "\0" /* DHCP_REQUESTED_IP */
88 "lease" "\0" /* DHCP_LEASE_TIME */
89 "dhcptype" "\0" /* */
90 "serverid" "\0" /* DHCP_SERVER_ID */
Denis Vlasenkob539c842007-11-29 08:17:45 +000091 "message" "\0" /* DHCP_MESSAGE */
92 "vendorclass" "\0" /* DHCP_VENDOR */
93 "clientid" "\0" /* DHCP_CLIENT_ID */
94 "tftp" "\0"
95 "bootfile" "\0"
96 "userclass" "\0"
Denis Vlasenko35a064b2008-11-06 00:49:59 +000097#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenkob539c842007-11-29 08:17:45 +000098 "search" "\0"
99#endif
100 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
101 "wpad" "\0"
102 ;
103
Denis Vlasenko35ff7462007-11-28 19:23:12 +0000104
Mike Frysinger7031f622006-05-08 03:20:50 +0000105/* Lengths of the different option types */
Denis Vlasenkob539c842007-11-29 08:17:45 +0000106const uint8_t dhcp_option_lengths[] ALIGN1 = {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000107 [OPTION_IP] = 4,
108 [OPTION_IP_PAIR] = 8,
109 [OPTION_BOOLEAN] = 1,
110 [OPTION_STRING] = 1,
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000111#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko50664732007-02-27 21:15:08 +0000112 [OPTION_STR1035] = 1,
113#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000114 [OPTION_U8] = 1,
115 [OPTION_U16] = 2,
116 [OPTION_S16] = 2,
117 [OPTION_U32] = 4,
118 [OPTION_S32] = 4
Mike Frysinger7031f622006-05-08 03:20:50 +0000119};
120
121
122/* get an option with bounds checking (warning, not aligned). */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000123uint8_t* FAST_FUNC get_option(struct dhcpMessage *packet, int code)
Mike Frysinger7031f622006-05-08 03:20:50 +0000124{
125 int i, length;
126 uint8_t *optionptr;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000127 int over = 0;
128 int curr = OPTION_FIELD;
Mike Frysinger7031f622006-05-08 03:20:50 +0000129
130 optionptr = packet->options;
131 i = 0;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000132 length = sizeof(packet->options);
133 while (1) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000134 if (i >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000135 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +0000136 return NULL;
137 }
138 if (optionptr[i + OPT_CODE] == code) {
139 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000140 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +0000141 return NULL;
142 }
143 return optionptr + i + 2;
144 }
145 switch (optionptr[i + OPT_CODE]) {
146 case DHCP_PADDING:
147 i++;
148 break;
149 case DHCP_OPTION_OVER:
150 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000151 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 return NULL;
153 }
154 over = optionptr[i + 3];
155 i += optionptr[OPT_LEN] + 2;
156 break;
157 case DHCP_END:
Denis Vlasenko6884f662007-11-23 00:08:54 +0000158 if (curr == OPTION_FIELD && (over & FILE_FIELD)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 optionptr = packet->file;
160 i = 0;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000161 length = sizeof(packet->file);
Mike Frysinger7031f622006-05-08 03:20:50 +0000162 curr = FILE_FIELD;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000163 } else if (curr == FILE_FIELD && (over & SNAME_FIELD)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 optionptr = packet->sname;
165 i = 0;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000166 length = sizeof(packet->sname);
Mike Frysinger7031f622006-05-08 03:20:50 +0000167 curr = SNAME_FIELD;
Denis Vlasenko6884f662007-11-23 00:08:54 +0000168 } else
169 return NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000170 break;
171 default:
172 i += optionptr[OPT_LEN + i] + 2;
173 }
174 }
175 return NULL;
176}
177
178
179/* return the position of the 'end' option (no bounds checking) */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000180int FAST_FUNC end_option(uint8_t *optionptr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000181{
182 int i = 0;
183
184 while (optionptr[i] != DHCP_END) {
Denis Vlasenkob539c842007-11-29 08:17:45 +0000185 if (optionptr[i] == DHCP_PADDING)
186 i++;
187 else
188 i += optionptr[i + OPT_LEN] + 2;
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 }
190 return i;
191}
192
193
194/* add an option string to the options (an option string contains an option code,
195 * length, then data) */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000196int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
Mike Frysinger7031f622006-05-08 03:20:50 +0000197{
198 int end = end_option(optionptr);
199
200 /* end position + string length + option code/length + end option */
Denis Vlasenko72e76042007-11-25 03:15:24 +0000201 if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000202 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000203 string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000204 return 0;
205 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000206 DEBUG("adding option 0x%02x", string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000207 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
208 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
209 return string[OPT_LEN] + 2;
210}
211
212
213/* add a one to four byte option to a packet */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000214int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
Mike Frysinger7031f622006-05-08 03:20:50 +0000215{
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000216 const struct dhcp_option *dh;
Mike Frysinger7031f622006-05-08 03:20:50 +0000217
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000218 for (dh = dhcp_options; dh->code; dh++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000219 if (dh->code == code) {
220 uint8_t option[6], len;
221
222 option[OPT_CODE] = code;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000223 len = dhcp_option_lengths[dh->flags & TYPE_MASK];
Mike Frysinger7031f622006-05-08 03:20:50 +0000224 option[OPT_LEN] = len;
Denis Vlasenkob539c842007-11-29 08:17:45 +0000225 if (BB_BIG_ENDIAN)
226 data <<= 8 * (4 - len);
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000227 /* Assignment is unaligned! */
228 move_to_unaligned32(&option[OPT_DATA], data);
Mike Frysinger7031f622006-05-08 03:20:50 +0000229 return add_option_string(optionptr, option);
230 }
231 }
232
Denis Vlasenkoea620772006-10-14 02:23:43 +0000233 bb_error_msg("cannot add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000234 return 0;
235}