2 * options.c -- DHCP server option packet tools
3 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
15 /* supported options are easily added here */
16 struct dhcp_option options[] = {
17 /* name[10] flags code */
18 {"subnet", OPTION_IP | OPTION_REQ, 0x01},
19 {"timezone", OPTION_S32, 0x02},
20 {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
21 {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
22 {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
23 {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
24 {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
25 {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
26 {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
27 {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
28 {"bootsize", OPTION_U16, 0x0d},
29 {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
30 {"swapsvr", OPTION_IP, 0x10},
31 {"rootpath", OPTION_STRING, 0x11},
32 {"ipttl", OPTION_U8, 0x17},
33 {"mtu", OPTION_U16, 0x1a},
34 {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
35 {"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
36 {"wins", OPTION_IP | OPTION_LIST, 0x2c},
37 {"requestip", OPTION_IP, 0x32},
38 {"lease", OPTION_U32, 0x33},
39 {"dhcptype", OPTION_U8, 0x35},
40 {"serverid", OPTION_IP, 0x36},
41 {"message", OPTION_STRING, 0x38},
42 {"tftp", OPTION_STRING, 0x42},
43 {"bootfile", OPTION_STRING, 0x43},
47 /* Lengths of the different option types */
48 int option_lengths[] = {
61 /* get an option with bounds checking (warning, not aligned). */
62 unsigned char *get_option(struct dhcpMessage *packet, int code)
65 unsigned char *optionptr;
66 int over = 0, done = 0, curr = OPTION_FIELD;
68 optionptr = packet->options;
73 LOG(LOG_WARNING, "bogus packet, option fields too long.");
76 if (optionptr[i + OPT_CODE] == code) {
77 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
78 LOG(LOG_WARNING, "bogus packet, option fields too long.");
81 return optionptr + i + 2;
83 switch (optionptr[i + OPT_CODE]) {
87 case DHCP_OPTION_OVER:
88 if (i + 1 + optionptr[i + OPT_LEN] >= length) {
89 LOG(LOG_WARNING, "bogus packet, option fields too long.");
92 over = optionptr[i + 3];
93 i += optionptr[OPT_LEN] + 2;
96 if (curr == OPTION_FIELD && over & FILE_FIELD) {
97 optionptr = packet->file;
101 } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
102 optionptr = packet->sname;
109 i += optionptr[OPT_LEN + i] + 2;
116 /* return the position of the 'end' option (no bounds checking) */
117 int end_option(unsigned char *optionptr)
121 while (optionptr[i] != DHCP_END) {
122 if (optionptr[i] == DHCP_PADDING) i++;
123 else i += optionptr[i + OPT_LEN] + 2;
129 /* add an option string to the options (an option string contains an option code,
130 * length, then data) */
131 int add_option_string(unsigned char *optionptr, unsigned char *string)
133 int end = end_option(optionptr);
135 /* end position + string length + option code/length + end option */
136 if (end + string[OPT_LEN] + 2 + 1 >= 308) {
137 LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
140 DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
141 memcpy(optionptr + end, string, string[OPT_LEN] + 2);
142 optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
143 return string[OPT_LEN] + 2;
147 /* add a one to four byte option to a packet */
148 int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data)
152 unsigned char option[2 + 4];
157 u8 = (unsigned char *) &aligned;
158 u16 = (u_int16_t *) &aligned;
161 for (i = 0; options[i].code; i++)
162 if (options[i].code == code) {
163 length = option_lengths[options[i].flags & TYPE_MASK];
167 DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
171 option[OPT_CODE] = code;
172 option[OPT_LEN] = length;
175 case 1: *u8 = data; break;
176 case 2: *u16 = data; break;
177 case 4: *u32 = data; break;
179 memcpy(option + 2, &aligned, length);
180 return add_option_string(optionptr, option);
184 /* find option 'code' in opt_list */
185 struct option_set *find_option(struct option_set *opt_list, char code)
187 while (opt_list && opt_list->data[OPT_CODE] < code)
188 opt_list = opt_list->next;
190 if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
195 /* add an option to the opt_list */
196 void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
198 struct option_set *existing, *new, **curr;
200 /* add it to an existing option */
201 if ((existing = find_option(*opt_list, option->code))) {
202 DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
203 if (option->flags & OPTION_LIST) {
204 if (existing->data[OPT_LEN] + length <= 255) {
205 existing->data = realloc(existing->data,
206 existing->data[OPT_LEN] + length + 2);
207 memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
208 existing->data[OPT_LEN] += length;
209 } /* else, ignore the data, we could put this in a second option in the future */
210 } /* else, ignore the new data */
212 DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
214 /* make a new option */
215 new = xmalloc(sizeof(struct option_set));
216 new->data = xmalloc(length + 2);
217 new->data[OPT_CODE] = option->code;
218 new->data[OPT_LEN] = length;
219 memcpy(new->data + 2, buffer, length);
222 while (*curr && (*curr)->data[OPT_CODE] < option->code)
223 curr = &(*curr)->next;