blob: 3168fc69aa68d9e6d60c306b0831c7190934fc4f [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[] = {
Denis Vlasenko50664732007-02-27 21:15:08 +000014 /* name[12] flags code */
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000015 {"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 Vlasenko50664732007-02-27 21:15:08 +000046#if ENABLE_FEATURE_RFC3397
47 {"search", OPTION_STR1035 | OPTION_LIST | OPTION_REQ, 0x77},
48#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000049 /* MSIE's "Web Proxy Autodiscovery Protocol" support */
50 {"wpad", OPTION_STRING, 0xfc},
51 {"", 0x00, 0x00}
Mike Frysinger7031f622006-05-08 03:20:50 +000052};
53
54/* Lengths of the different option types */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055const unsigned char option_lengths[] ALIGN1 = {
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000056 [OPTION_IP] = 4,
57 [OPTION_IP_PAIR] = 8,
58 [OPTION_BOOLEAN] = 1,
59 [OPTION_STRING] = 1,
Denis Vlasenko50664732007-02-27 21:15:08 +000060#if ENABLE_FEATURE_RFC3397
61 [OPTION_STR1035] = 1,
62#endif
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +000063 [OPTION_U8] = 1,
64 [OPTION_U16] = 2,
65 [OPTION_S16] = 2,
66 [OPTION_U32] = 4,
67 [OPTION_S32] = 4
Mike Frysinger7031f622006-05-08 03:20:50 +000068};
69
70
71/* get an option with bounds checking (warning, not aligned). */
72uint8_t *get_option(struct dhcpMessage *packet, int code)
73{
74 int i, length;
75 uint8_t *optionptr;
76 int over = 0, done = 0, curr = OPTION_FIELD;
77
78 optionptr = packet->options;
79 i = 0;
80 length = 308;
81 while (!done) {
82 if (i >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000083 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000084 return NULL;
85 }
86 if (optionptr[i + OPT_CODE] == code) {
87 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000088 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +000089 return NULL;
90 }
91 return optionptr + i + 2;
92 }
93 switch (optionptr[i + OPT_CODE]) {
94 case DHCP_PADDING:
95 i++;
96 break;
97 case DHCP_OPTION_OVER:
98 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000099 bb_error_msg("bogus packet, option fields too long");
Mike Frysinger7031f622006-05-08 03:20:50 +0000100 return NULL;
101 }
102 over = optionptr[i + 3];
103 i += optionptr[OPT_LEN] + 2;
104 break;
105 case DHCP_END:
106 if (curr == OPTION_FIELD && over & FILE_FIELD) {
107 optionptr = packet->file;
108 i = 0;
109 length = 128;
110 curr = FILE_FIELD;
111 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
112 optionptr = packet->sname;
113 i = 0;
114 length = 64;
115 curr = SNAME_FIELD;
116 } else done = 1;
117 break;
118 default:
119 i += optionptr[OPT_LEN + i] + 2;
120 }
121 }
122 return NULL;
123}
124
125
126/* return the position of the 'end' option (no bounds checking) */
127int end_option(uint8_t *optionptr)
128{
129 int i = 0;
130
131 while (optionptr[i] != DHCP_END) {
132 if (optionptr[i] == DHCP_PADDING) i++;
133 else i += optionptr[i + OPT_LEN] + 2;
134 }
135 return i;
136}
137
138
139/* add an option string to the options (an option string contains an option code,
140 * length, then data) */
141int add_option_string(uint8_t *optionptr, uint8_t *string)
142{
143 int end = end_option(optionptr);
144
145 /* end position + string length + option code/length + end option */
146 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000147 bb_error_msg("option 0x%02x did not fit into the packet",
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000148 string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 return 0;
150 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000151 DEBUG("adding option 0x%02x", string[OPT_CODE]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
153 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
154 return string[OPT_LEN] + 2;
155}
156
157
158/* add a one to four byte option to a packet */
159int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
160{
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000161 const struct dhcp_option *dh;
Mike Frysinger7031f622006-05-08 03:20:50 +0000162
Denis Vlasenkoc90c3f32006-11-23 12:57:49 +0000163 for (dh = dhcp_options; dh->code; dh++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 if (dh->code == code) {
165 uint8_t option[6], len;
166
167 option[OPT_CODE] = code;
168 len = option_lengths[dh->flags & TYPE_MASK];
169 option[OPT_LEN] = len;
Rob Landleyc8b360e2006-05-31 22:24:33 +0000170 if (BB_BIG_ENDIAN) data <<= 8 * (4 - len);
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 /* This memcpy is for broken processors which can't
172 * handle a simple unaligned 32-bit assignment */
173 memcpy(&option[OPT_DATA], &data, 4);
174 return add_option_string(optionptr, option);
175 }
176 }
177
Denis Vlasenkoea620772006-10-14 02:23:43 +0000178 bb_error_msg("cannot add option 0x%02x", code);
Mike Frysinger7031f622006-05-08 03:20:50 +0000179 return 0;
180}