blob: bda6efc7e5cebb37a78f0a44faaefd44881590a9 [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
5 */
6
Mike Frysinger7031f622006-05-08 03:20:50 +00007#include "common.h"
8#include "dhcpd.h"
9#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000010
11
12/* supported options are easily added here */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000013const struct dhcp_option dhcp_options[] = {
14 /* name[10] flags code */
15 {"subnet", OPTION_IP | OPTION_REQ, 0x01},
16 {"timezone", OPTION_S32, 0x02},
17 {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
18 {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
19 {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
20 {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
21 {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
22 {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
23 {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
24 {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
25 {"bootsize", OPTION_U16, 0x0d},
Denis Vlasenkoc4d606e2007-02-02 01:16:08 +000026 {"domain", OPTION_STRING | OPTION_LIST | OPTION_REQ, 0x0f},
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000027 {"swapsvr", OPTION_IP, 0x10},
28 {"rootpath", OPTION_STRING, 0x11},
29 {"ipttl", OPTION_U8, 0x17},
30 {"mtu", OPTION_U16, 0x1a},
31 {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
32 {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28},
33 {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29},
34 {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a},
35 {"wins", OPTION_IP | OPTION_LIST, 0x2c},
36 {"requestip", OPTION_IP, 0x32},
37 {"lease", OPTION_U32, 0x33},
38 {"dhcptype", OPTION_U8, 0x35},
39 {"serverid", OPTION_IP, 0x36},
40 {"message", OPTION_STRING, 0x38},
Denis Vlasenkoe175ff22006-09-26 17:41:00 +000041 {"vendorclass", OPTION_STRING, 0x3C},
42 {"clientid", OPTION_STRING, 0x3D},
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000043 {"tftp", OPTION_STRING, 0x42},
44 {"bootfile", OPTION_STRING, 0x43},
Denis Vlasenkoe175ff22006-09-26 17:41:00 +000045 {"userclass", OPTION_STRING, 0x4D},
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000046 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
47 {"wpad", OPTION_STRING, 0xfc},
48 {"", 0x00, 0x00}
Mike Frysinger7031f622006-05-08 03:20:50 +000049};
50
51/* Lengths of the different option types */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000052const unsigned char option_lengths[] = {
53 [OPTION_IP] = 4,
54 [OPTION_IP_PAIR] = 8,
55 [OPTION_BOOLEAN] = 1,
56 [OPTION_STRING] = 1,
57 [OPTION_U8] = 1,
58 [OPTION_U16] = 2,
59 [OPTION_S16] = 2,
60 [OPTION_U32] = 4,
61 [OPTION_S32] = 4
Mike Frysinger7031f622006-05-08 03:20:50 +000062};
63
64
65/* get an option with bounds checking (warning, not aligned). */
66uint8_t *get_option(struct dhcpMessage *packet, int code)
67{
68 int i, length;
69 uint8_t *optionptr;
70 int over = 0, done = 0, curr = OPTION_FIELD;
71
72 optionptr = packet->options;
73 i = 0;
74 length = 308;
75 while (!done) {
76 if (i >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000077 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000078 return NULL;
79 }
80 if (optionptr[i + OPT_CODE] == code) {
81 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000082 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000083 return NULL;
84 }
85 return optionptr + i + 2;
86 }
87 switch (optionptr[i + OPT_CODE]) {
88 case DHCP_PADDING:
89 i++;
90 break;
91 case DHCP_OPTION_OVER:
92 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000093 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000094 return NULL;
95 }
96 over = optionptr[i + 3];
97 i += optionptr[OPT_LEN] + 2;
98 break;
99 case DHCP_END:
100 if (curr == OPTION_FIELD && over & FILE_FIELD) {
101 optionptr = packet->file;
102 i = 0;
103 length = 128;
104 curr = FILE_FIELD;
105 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
106 optionptr = packet->sname;
107 i = 0;
108 length = 64;
109 curr = SNAME_FIELD;
110 } else done = 1;
111 break;
112 default:
113 i += optionptr[OPT_LEN + i] + 2;
114 }
115 }
116 return NULL;
117}
118
119
120/* return the position of the 'end' option (no bounds checking) */
121int end_option(uint8_t *optionptr)
122{
123 int i = 0;
124
125 while (optionptr[i] != DHCP_END) {
126 if (optionptr[i] == DHCP_PADDING) i++;
127 else i += optionptr[i + OPT_LEN] + 2;
128 }
129 return i;
130}
131
132
133/* add an option string to the options (an option string contains an option code,
134 * length, then data) */
135int add_option_string(uint8_t *optionptr, uint8_t *string)
136{
137 int end = end_option(optionptr);
138
139 /* end position + string length + option code/length + end option */
140 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000141 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000142 string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 return 0;
144 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000145 DEBUG("adding option 0x%02x", string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
147 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
148 return string[OPT_LEN] + 2;
149}
150
151
152/* add a one to four byte option to a packet */
153int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
154{
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000155 const struct dhcp_option *dh;
Mike Frysinger7031f622006-05-08 03:20:50 +0000156
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000157 for (dh = dhcp_options; dh->code; dh++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 if (dh->code == code) {
159 uint8_t option[6], len;
160
161 option[OPT_CODE] = code;
162 len = option_lengths[dh->flags & TYPE_MASK];
163 option[OPT_LEN] = len;
Rob Landleyc8b360e2006-05-31 22:24:33 +0000164 if (BB_BIG_ENDIAN) data <<= 8 * (4 - len);
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 /* This memcpy is for broken processors which can't
166 * handle a simple unaligned 32-bit assignment */
167 memcpy(&option[OPT_DATA], &data, 4);
168 return add_option_string(optionptr, option);
169 }
170 }
171
Denis Vlasenkoea620772006-10-14 02:23:43 +0000172 bb_error_msg("cannot add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 return 0;
174}