1 /* $OpenBSD: options.c,v 1.15 2004/12/26 03:17:07 deraadt Exp $ */
3 /* DHCP options parsing and reassembly. */
6 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
48 #define DHCP_OPTION_DATA
52 int bad_options_max = 5;
54 void parse_options(struct packet *);
55 void parse_option_buffer(struct packet *, unsigned char *, int);
56 unsigned store_options(unsigned char *, int, struct tree_cache **,
57 unsigned char *, int, int, int, int);
58 void expand_domain_search(struct packet *packet);
59 int find_search_domain_name_len(struct option_data *option, size_t *offset);
60 void expand_search_domain_name(struct option_data *option, size_t *offset,
61 unsigned char **domain_search);
65 * Parse all available options out of the specified packet.
68 parse_options(struct packet *packet)
70 /* Initially, zero all option pointers. */
71 memset(packet->options, 0, sizeof(packet->options));
73 /* If we don't see the magic cookie, there's nothing to parse. */
74 if (memcmp(packet->raw->options, DHCP_OPTIONS_COOKIE, 4)) {
75 packet->options_valid = 0;
80 * Go through the options field, up to the end of the packet or
83 parse_option_buffer(packet, &packet->raw->options[4],
84 packet->packet_length - DHCP_FIXED_NON_UDP - 4);
87 * If we parsed a DHCP Option Overload option, parse more
88 * options out of the buffer(s) containing them.
90 if (packet->options_valid &&
91 packet->options[DHO_DHCP_OPTION_OVERLOAD].data) {
92 if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1)
93 parse_option_buffer(packet,
94 (unsigned char *)packet->raw->file,
95 sizeof(packet->raw->file));
96 if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2)
97 parse_option_buffer(packet,
98 (unsigned char *)packet->raw->sname,
99 sizeof(packet->raw->sname));
102 /* Expand DHCP Domain Search option. */
103 if (packet->options_valid) {
104 expand_domain_search(packet);
109 * Parse options out of the specified buffer, storing addresses of
110 * option values in packet->options and setting packet->options_valid if
111 * no errors are encountered.
114 parse_option_buffer(struct packet *packet,
115 unsigned char *buffer, int length)
117 unsigned char *s, *t, *end = buffer + length;
120 for (s = buffer; *s != DHO_END && s < end; ) {
123 /* Pad options don't have a length - just skip them. */
124 if (code == DHO_PAD) {
134 * All other fields (except end, see above) have a
140 * If the length is outrageous, silently skip the rest,
141 * and mark the packet bad. Unfortunately some crappy
142 * dhcp servers always seem to give us garbage on the
143 * end of a packet. so rather than keep refusing, give
144 * up and try to take one after seeing a few without
147 if (s + len + 2 > end) {
150 warning("option %s (%d) %s.",
151 dhcp_options[code].name, len,
152 "larger than buffer");
153 if (bad_options == bad_options_max) {
154 packet->options_valid = 1;
156 warning("Many bogus options seen in offers. "
157 "Taking this offer in spite of bogus "
158 "options - hope for the best!");
160 warning("rejecting bogus offer.");
161 packet->options_valid = 0;
166 * If we haven't seen this option before, just make
167 * space for it and copy it there.
169 if (!packet->options[code].data) {
170 if (!(t = calloc(1, len + 1)))
171 error("Can't allocate storage for option %s.",
172 dhcp_options[code].name);
174 * Copy and NUL-terminate the option (in case
175 * it's an ASCII string.
177 memcpy(t, &s[2], len);
179 packet->options[code].len = len;
180 packet->options[code].data = t;
183 * If it's a repeat, concatenate it to whatever
184 * we last saw. This is really only required
185 * for clients, but what the heck...
187 t = calloc(1, len + packet->options[code].len + 1);
189 error("Can't expand storage for option %s.",
190 dhcp_options[code].name);
191 memcpy(t, packet->options[code].data,
192 packet->options[code].len);
193 memcpy(t + packet->options[code].len,
195 packet->options[code].len += len;
196 t[packet->options[code].len] = 0;
197 free(packet->options[code].data);
198 packet->options[code].data = t;
202 packet->options_valid = 1;
206 * Expand DHCP Domain Search option. The value of this option is
207 * encoded like DNS' list of labels. See:
212 expand_domain_search(struct packet *packet)
215 int expanded_len, next_domain_len;
216 struct option_data *option;
217 unsigned char *domain_search, *cursor;
219 if (packet->options[DHO_DOMAIN_SEARCH].data == NULL)
222 option = &packet->options[DHO_DOMAIN_SEARCH];
224 /* Compute final expanded length. */
227 while (offset < option->len) {
228 next_domain_len = find_search_domain_name_len(option, &offset);
229 if (next_domain_len < 0)
230 /* The Domain Search option value is invalid. */
233 /* We add 1 for the space between domain names. */
234 expanded_len += next_domain_len + 1;
236 if (expanded_len > 0)
237 /* Remove 1 for the superfluous trailing space. */
240 domain_search = malloc(expanded_len + 1);
241 if (domain_search == NULL)
242 error("Can't allocate storage for expanded domain-search\n");
245 cursor = domain_search;
246 while (offset < option->len) {
247 expand_search_domain_name(option, &offset, &cursor);
251 domain_search[expanded_len] = '\0';
254 option->len = expanded_len;
255 option->data = domain_search;
259 find_search_domain_name_len(struct option_data *option, size_t *offset)
261 int domain_name_len, label_len, pointed_len;
267 while (i < option->len) {
268 label_len = option->data[i];
269 if (label_len == 0) {
271 * A zero-length label marks the end of this
275 return (domain_name_len);
276 } else if (label_len & 0xC0) {
277 /* This is a pointer to another list of labels. */
278 if (i + 1 >= option->len) {
279 /* The pointer is truncated. */
280 warning("Truncated pointer in DHCP Domain "
285 pointer = ((label_len & ~(0xC0)) << 8) +
287 if (pointer >= *offset) {
289 * The pointer must indicate a prior
292 warning("Invalid forward pointer in DHCP "
293 "Domain Search option compression.");
297 pointed_len = find_search_domain_name_len(option,
299 domain_name_len += pointed_len;
302 return (domain_name_len);
305 if (i + label_len >= option->len) {
306 warning("Truncated label in DHCP Domain Search "
312 * Update the domain name length with the length of the
313 * current label, plus a trailing dot ('.').
315 domain_name_len += label_len + 1;
321 warning("Truncated DHCP Domain Search option.");
327 expand_search_domain_name(struct option_data *option, size_t *offset,
328 unsigned char **domain_search)
332 unsigned char *cursor;
335 * This is the same loop than the function above
336 * (find_search_domain_name_len). Therefore, we remove checks,
337 * they're already done. Here, we just make the copy.
340 cursor = *domain_search;
341 while (i < option->len) {
342 label_len = option->data[i];
343 if (label_len == 0) {
345 * A zero-length label marks the end of this
349 *domain_search = cursor;
351 } else if (label_len & 0xC0) {
352 /* This is a pointer to another list of labels. */
353 pointer = ((label_len & ~(0xC0)) << 8) +
356 expand_search_domain_name(option, &pointer, &cursor);
359 *domain_search = cursor;
363 /* Copy the label found. */
364 memcpy(cursor, option->data + i + 1, label_len);
365 cursor[label_len] = '.';
369 cursor += label_len + 1;
374 * cons options into a big buffer, and then split them out into the
375 * three separate buffers if needed. This allows us to cons up a set of
376 * vendor options using the same routine.
379 cons_options(struct packet *inpacket, struct dhcp_packet *outpacket,
380 int mms, struct tree_cache **options,
381 int overload, /* Overload flags that may be set. */
382 int terminate, int bootpp, u_int8_t *prl, int prl_len)
384 unsigned char priority_list[300], buffer[4096];
385 unsigned priority_len;
386 size_t main_buffer_size;
387 unsigned option_size, bufix, mainbufix;
391 * If the client has provided a maximum DHCP message size, use
392 * that; otherwise, if it's BOOTP, only 64 bytes; otherwise use
393 * up to the minimum IP MTU size (576 bytes).
395 * XXX if a BOOTP client specifies a max message size, we will
400 inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data &&
401 (inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].len >=
404 inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data);
407 main_buffer_size = mms - DHCP_FIXED_LEN;
409 main_buffer_size = 64;
411 main_buffer_size = 576 - DHCP_FIXED_LEN;
413 if (main_buffer_size > sizeof(buffer))
414 main_buffer_size = sizeof(buffer);
416 /* Preload the option priority list with mandatory options. */
418 priority_list[priority_len++] = DHO_DHCP_MESSAGE_TYPE;
419 priority_list[priority_len++] = DHO_DHCP_SERVER_IDENTIFIER;
420 priority_list[priority_len++] = DHO_DHCP_LEASE_TIME;
421 priority_list[priority_len++] = DHO_DHCP_MESSAGE;
424 * If the client has provided a list of options that it wishes
425 * returned, use it to prioritize. Otherwise, prioritize based
426 * on the default priority list.
429 inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data) {
431 inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].len;
432 if (prlen + priority_len > sizeof(priority_list))
433 prlen = sizeof(priority_list) - priority_len;
435 memcpy(&priority_list[priority_len],
436 inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data,
438 priority_len += prlen;
441 if (prl_len + priority_len > sizeof(priority_list))
442 prl_len = sizeof(priority_list) - priority_len;
444 memcpy(&priority_list[priority_len], prl, prl_len);
445 priority_len += prl_len;
448 memcpy(&priority_list[priority_len],
449 dhcp_option_default_priority_list,
450 sizeof_dhcp_option_default_priority_list);
451 priority_len += sizeof_dhcp_option_default_priority_list;
454 /* Copy the options into the big buffer... */
455 option_size = store_options(
457 (main_buffer_size - 7 + ((overload & 1) ? DHCP_FILE_LEN : 0) +
458 ((overload & 2) ? DHCP_SNAME_LEN : 0)),
459 options, priority_list, priority_len, main_buffer_size,
460 (main_buffer_size + ((overload & 1) ? DHCP_FILE_LEN : 0)),
463 /* Put the cookie up front... */
464 memcpy(outpacket->options, DHCP_OPTIONS_COOKIE, 4);
468 * If we're going to have to overload, store the overload option
469 * at the beginning. If we can, though, just store the whole
470 * thing in the packet's option buffer and leave it at that.
472 if (option_size <= main_buffer_size - mainbufix) {
473 memcpy(&outpacket->options[mainbufix],
474 buffer, option_size);
475 mainbufix += option_size;
476 if (mainbufix < main_buffer_size)
477 outpacket->options[mainbufix++] = DHO_END;
478 length = DHCP_FIXED_NON_UDP + mainbufix;
480 outpacket->options[mainbufix++] = DHO_DHCP_OPTION_OVERLOAD;
481 outpacket->options[mainbufix++] = 1;
483 main_buffer_size - mainbufix + DHCP_FILE_LEN)
484 outpacket->options[mainbufix++] = 3;
486 outpacket->options[mainbufix++] = 1;
488 memcpy(&outpacket->options[mainbufix],
489 buffer, main_buffer_size - mainbufix);
490 bufix = main_buffer_size - mainbufix;
491 length = DHCP_FIXED_NON_UDP + mainbufix;
493 if (option_size - bufix <= DHCP_FILE_LEN) {
494 memcpy(outpacket->file,
495 &buffer[bufix], option_size - bufix);
496 mainbufix = option_size - bufix;
497 if (mainbufix < DHCP_FILE_LEN)
498 outpacket->file[mainbufix++] = (char)DHO_END;
499 while (mainbufix < DHCP_FILE_LEN)
500 outpacket->file[mainbufix++] = (char)DHO_PAD;
502 memcpy(outpacket->file,
503 &buffer[bufix], DHCP_FILE_LEN);
504 bufix += DHCP_FILE_LEN;
507 if ((overload & 2) && option_size < bufix) {
508 memcpy(outpacket->sname,
509 &buffer[bufix], option_size - bufix);
511 mainbufix = option_size - bufix;
512 if (mainbufix < DHCP_SNAME_LEN)
513 outpacket->file[mainbufix++] = (char)DHO_END;
514 while (mainbufix < DHCP_SNAME_LEN)
515 outpacket->file[mainbufix++] = (char)DHO_PAD;
522 * Store all the requested options into the requested buffer.
525 store_options(unsigned char *buffer, int buflen, struct tree_cache **options,
526 unsigned char *priority_list, int priority_len, int first_cutoff,
527 int second_cutoff, int terminate)
529 int bufix = 0, option_stored[256], i, ix, tto;
531 /* Zero out the stored-lengths array. */
532 memset(option_stored, 0, sizeof(option_stored));
535 * Copy out the options in the order that they appear in the
538 for (i = 0; i < priority_len; i++) {
539 /* Code for next option to try to store. */
540 int code = priority_list[i];
544 * Number of bytes left to store (some may already have
545 * been stored by a previous pass).
549 /* If no data is available for this option, skip it. */
550 if (!options[code]) {
555 * The client could ask for things that are mandatory,
556 * in which case we should avoid storing them twice...
558 if (option_stored[code])
560 option_stored[code] = 1;
562 /* We should now have a constant length for the option. */
563 length = options[code]->len;
565 /* Do we add a NUL? */
566 if (terminate && dhcp_options[code].format[0] == 't') {
572 /* Try to store the option. */
575 * If the option's length is more than 255, we must
576 * store it in multiple hunks. Store 255-byte hunks
577 * first. However, in any case, if the option data will
578 * cross a buffer boundary, split it across that
585 unsigned char incr = length > 255 ? 255 : length;
588 * If this hunk of the buffer will cross a
589 * boundary, only go up to the boundary in this
592 if (bufix < first_cutoff &&
593 bufix + incr > first_cutoff)
594 incr = first_cutoff - bufix;
595 else if (bufix < second_cutoff &&
596 bufix + incr > second_cutoff)
597 incr = second_cutoff - bufix;
600 * If this option is going to overflow the
603 if (bufix + 2 + incr > buflen) {
608 /* Everything looks good - copy it in! */
609 buffer[bufix] = code;
610 buffer[bufix + 1] = incr;
611 if (tto && incr == length) {
612 memcpy(buffer + bufix + 2,
613 options[code]->value + ix, incr - 1);
614 buffer[bufix + 2 + incr - 1] = 0;
616 memcpy(buffer + bufix + 2,
617 options[code]->value + ix, incr);
627 * Format the specified option so that a human can easily read it.
630 pretty_print_option(unsigned int code, unsigned char *data, int len,
631 int emit_commas, int emit_quotes)
633 static char optbuf[32768]; /* XXX */
634 int hunksize = 0, numhunk = -1, numelem = 0;
635 char fmtbuf[32], *op = optbuf;
636 int i, j, k, opleft = sizeof(optbuf);
637 unsigned char *dp = data;
641 /* Code should be between 0 and 255. */
643 error("pretty_print_option: bad code %d", code);
650 /* Figure out the size of the data. */
651 for (i = 0; dhcp_options[code].format[i]; i++) {
653 warning("%s: Excess information in format string: %s",
654 dhcp_options[code].name,
655 &(dhcp_options[code].format[i]));
659 fmtbuf[i] = dhcp_options[code].format[i];
660 switch (dhcp_options[code].format[i]) {
667 for (k = 0; k < len; k++)
668 if (!isascii(data[k]) ||
704 warning("%s: garbage in format string: %s",
705 dhcp_options[code].name,
706 &(dhcp_options[code].format[i]));
711 /* Check for too few bytes... */
712 if (hunksize > len) {
713 warning("%s: expecting at least %d bytes; got %d",
714 dhcp_options[code].name, hunksize, len);
717 /* Check for too many bytes... */
718 if (numhunk == -1 && hunksize < len)
719 warning("%s: %d extra bytes",
720 dhcp_options[code].name, len - hunksize);
722 /* If this is an array, compute its size. */
724 numhunk = len / hunksize;
725 /* See if we got an exact number of hunks. */
726 if (numhunk > 0 && numhunk * hunksize < len)
727 warning("%s: %d extra bytes at end of array",
728 dhcp_options[code].name, len - numhunk * hunksize);
730 /* A one-hunk array prints the same as a single hunk. */
734 /* Cycle through the array (or hunk) printing the data. */
735 for (i = 0; i < numhunk; i++) {
736 for (j = 0; j < numelem; j++) {
744 for (; dp < data + len; dp++) {
747 if (dp + 1 != data + len ||
754 } else if (*dp == '"' ||
775 foo.s_addr = htonl(getULong(dp));
776 opcount = strlcpy(op, inet_ntoa(foo), opleft);
777 if (opcount >= opleft)
783 opcount = snprintf(op, opleft, "%ld",
785 if (opcount >= opleft || opcount == -1)
791 opcount = snprintf(op, opleft, "%lu",
792 (unsigned long)getULong(dp));
793 if (opcount >= opleft || opcount == -1)
799 opcount = snprintf(op, opleft, "%d",
801 if (opcount >= opleft || opcount == -1)
807 opcount = snprintf(op, opleft, "%u",
809 if (opcount >= opleft || opcount == -1)
815 opcount = snprintf(op, opleft, "%d",
817 if (opcount >= opleft || opcount == -1)
822 opcount = snprintf(op, opleft, "%d", *dp++);
823 if (opcount >= opleft || opcount == -1)
828 opcount = snprintf(op, opleft, "%x", *dp++);
829 if (opcount >= opleft || opcount == -1)
834 opcount = strlcpy(op,
835 *dp++ ? "true" : "false", opleft);
836 if (opcount >= opleft)
841 warning("Unexpected format code %c", fmtbuf[j]);
844 opleft -= strlen(op);
847 if (j + 1 < numelem && comma != ':') {
852 if (i + 1 < numhunk) {
862 warning("dhcp option too large");
867 do_packet(struct interface_info *interface, struct dhcp_packet *packet,
868 int len, unsigned int from_port, struct iaddr from, struct hardware *hfrom)
873 if (packet->hlen > sizeof(packet->chaddr)) {
874 note("Discarding packet with invalid hlen.");
878 memset(&tp, 0, sizeof(tp));
880 tp.packet_length = len;
881 tp.client_port = from_port;
882 tp.client_addr = from;
883 tp.interface = interface;
887 if (tp.options_valid &&
888 tp.options[DHO_DHCP_MESSAGE_TYPE].data)
889 tp.packet_type = tp.options[DHO_DHCP_MESSAGE_TYPE].data[0];
895 /* Free the data associated with the options. */
896 for (i = 0; i < 256; i++)
897 if (tp.options[i].len && tp.options[i].data)
898 free(tp.options[i].data);