1 /* $OpenBSD: options.c,v 1.26 2010/01/02 04:21:16 krw Exp $ */ 2 3 /* DHCP options parsing and reassembly. */ 4 5 /* 6 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 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. 21 * 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 34 * SUCH DAMAGE. 35 * 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''. 41 */ 42 43 #include <ctype.h> 44 45 #include "dhcpd.h" 46 47 int bad_options = 0; 48 int bad_options_max = 5; 49 50 void parse_options(struct packet *); 51 void parse_option_buffer(struct packet *, unsigned char *, int); 52 void create_priority_list(unsigned char *, unsigned char *, int); 53 int store_option_fragment(unsigned char *, int, unsigned char, 54 int, unsigned char *); 55 int store_options(unsigned char *, int, struct tree_cache **, 56 unsigned char *, int, int); 57 58 59 /* 60 * Parse all available options out of the specified packet. 61 */ 62 void 63 parse_options(struct packet *packet) 64 { 65 /* Initially, zero all option pointers. */ 66 memset(packet->options, 0, sizeof(packet->options)); 67 68 /* If we don't see the magic cookie, there's nothing to parse. */ 69 if (memcmp(packet->raw->options, DHCP_OPTIONS_COOKIE, 4)) { 70 packet->options_valid = 0; 71 return; 72 } 73 74 /* 75 * Go through the options field, up to the end of the packet or 76 * the End field. 77 */ 78 parse_option_buffer(packet, &packet->raw->options[4], 79 packet->packet_length - DHCP_FIXED_NON_UDP - 4); 80 81 /* 82 * If we parsed a DHCP Option Overload option, parse more 83 * options out of the buffer(s) containing them. 84 */ 85 if (packet->options_valid && 86 packet->options[DHO_DHCP_OPTION_OVERLOAD].data) { 87 if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1) 88 parse_option_buffer(packet, 89 (unsigned char *)packet->raw->file, 90 sizeof(packet->raw->file)); 91 if (packet->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2) 92 parse_option_buffer(packet, 93 (unsigned char *)packet->raw->sname, 94 sizeof(packet->raw->sname)); 95 } 96 } 97 98 /* 99 * Parse options out of the specified buffer, storing addresses of 100 * option values in packet->options and setting packet->options_valid if 101 * no errors are encountered. 102 */ 103 void 104 parse_option_buffer(struct packet *packet, 105 unsigned char *buffer, int length) 106 { 107 unsigned char *s, *t; 108 unsigned char *end = buffer + length; 109 int len; 110 int code; 111 112 for (s = buffer; *s != DHO_END && s < end; ) { 113 code = s[0]; 114 115 /* Pad options don't have a length - just skip them. */ 116 if (code == DHO_PAD) { 117 s++; 118 continue; 119 } 120 if (s + 2 > end) { 121 len = 65536; 122 goto bogus; 123 } 124 125 /* 126 * All other fields (except end, see above) have a 127 * one-byte length. 128 */ 129 len = s[1]; 130 131 /* 132 * If the length is outrageous, silently skip the rest, 133 * and mark the packet bad. Unfortunately some crappy 134 * dhcp servers always seem to give us garbage on the 135 * end of a packet. so rather than keep refusing, give 136 * up and try to take one after seeing a few without 137 * anything good. 138 */ 139 if (s + len + 2 > end) { 140 bogus: 141 bad_options++; 142 warning("option %s (%d) %s.", 143 dhcp_options[code].name, len, 144 "larger than buffer"); 145 if (bad_options == bad_options_max) { 146 packet->options_valid = 1; 147 bad_options = 0; 148 warning("Many bogus options seen in offers."); 149 warning("Taking this offer in spite of bogus"); 150 warning("options - hope for the best!"); 151 } else { 152 warning("rejecting bogus offer."); 153 packet->options_valid = 0; 154 } 155 return; 156 } 157 /* 158 * If we haven't seen this option before, just make 159 * space for it and copy it there. 160 */ 161 if (!packet->options[code].data) { 162 t = calloc(1, len + 1); 163 if (!t) 164 error("Can't allocate storage for option %s.", 165 dhcp_options[code].name); 166 /* 167 * Copy and NUL-terminate the option (in case 168 * it's an ASCII string). 169 */ 170 memcpy(t, &s[2], len); 171 t[len] = 0; 172 packet->options[code].len = len; 173 packet->options[code].data = t; 174 } else { 175 /* 176 * If it's a repeat, concatenate it to whatever 177 * we last saw. This is really only required 178 * for clients, but what the heck... 179 */ 180 t = calloc(1, len + packet->options[code].len + 1); 181 if (!t) 182 error("Can't expand storage for option %s.", 183 dhcp_options[code].name); 184 memcpy(t, packet->options[code].data, 185 packet->options[code].len); 186 memcpy(t + packet->options[code].len, 187 &s[2], len); 188 packet->options[code].len += len; 189 t[packet->options[code].len] = 0; 190 free(packet->options[code].data); 191 packet->options[code].data = t; 192 } 193 s += len + 2; 194 } 195 packet->options_valid = 1; 196 } 197 198 /* 199 * Fill priority_list with a complete list of DHCP options sorted by 200 * priority. i.e. 201 * 1) Mandatory options. 202 * 2) Options from prl that are not already present. 203 * 3) Options from the default list that are not already present. 204 */ 205 void 206 create_priority_list(unsigned char *priority_list, unsigned char *prl, 207 int prl_len) 208 { 209 unsigned char stored_list[256]; 210 int i, priority_len = 0; 211 212 /* clear stored_list, priority_list should be cleared before */ 213 bzero(&stored_list, sizeof(stored_list)); 214 215 /* Some options we don't want on the priority list. */ 216 stored_list[DHO_PAD] = 1; 217 stored_list[DHO_END] = 1; 218 219 /* Mandatory options. */ 220 for(i = 0; dhcp_option_default_priority_list[i] != DHO_END; i++) { 221 priority_list[priority_len++] = 222 dhcp_option_default_priority_list[i]; 223 stored_list[dhcp_option_default_priority_list[i]] = 1; 224 } 225 226 /* Supplied priority list. */ 227 if (!prl) 228 prl_len = 0; 229 for(i = 0; i < prl_len; i++) { 230 if (stored_list[prl[i]]) 231 continue; 232 priority_list[priority_len++] = prl[i]; 233 stored_list[prl[i]] = 1; 234 } 235 236 /* Default priority list. */ 237 prl = dhcp_option_default_priority_list; 238 for(i = 0; i < 256; i++) { 239 if (stored_list[prl[i]]) 240 continue; 241 priority_list[priority_len++] = prl[i]; 242 stored_list[prl[i]] = 1; 243 } 244 } 245 /* 246 * cons options into a big buffer, and then split them out into the 247 * three separate buffers if needed. This allows us to cons up a set of 248 * vendor options using the same routine. 249 */ 250 int 251 cons_options(struct packet *inpacket, struct dhcp_packet *outpacket, 252 int mms, struct tree_cache **options, 253 int overload, /* Overload flags that may be set. */ 254 int terminate, int bootpp, u_int8_t *prl, int prl_len) 255 { 256 unsigned char priority_list[256]; 257 unsigned char buffer[4096]; /* Really big buffer... */ 258 int bufix, main_buffer_size, option_size; 259 260 /* 261 * If the client has provided a maximum DHCP message size, use 262 * that; otherwise, if it's BOOTP, only 64 bytes; otherwise use 263 * up to the minimum IP MTU size (576 bytes). 264 * 265 * XXX if a BOOTP client specifies a max message size, we will 266 * honor it. 267 */ 268 if (!mms && 269 inpacket && 270 inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data && 271 (inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].len >= 272 sizeof(u_int16_t))) { 273 mms = getUShort( 274 inpacket->options[DHO_DHCP_MAX_MESSAGE_SIZE].data); 275 } 276 277 if (mms) { 278 if (mms < 576) 279 mms = 576; /* mms must be >= minimum IP MTU */ 280 main_buffer_size = mms - DHCP_FIXED_LEN; 281 } else if (bootpp) 282 main_buffer_size = 64; 283 else 284 main_buffer_size = 576 - DHCP_FIXED_LEN; 285 286 if (main_buffer_size > sizeof(outpacket->options)) 287 main_buffer_size = sizeof(outpacket->options); 288 289 /* 290 * Initialize the available buffers, some or all of which may not be 291 * used. 292 */ 293 memset(outpacket->options, DHO_PAD, sizeof(outpacket->options)); 294 if (overload & 1) 295 memset(outpacket->file, DHO_PAD, DHCP_FILE_LEN); 296 if (overload & 2) 297 memset(outpacket->sname, DHO_PAD, DHCP_SNAME_LEN); 298 if (bootpp) 299 overload = 0; /* Don't use overload buffers for bootp! */ 300 301 /* 302 * Get complete list of possible options in priority order. Use the 303 * list provided in the options. Lacking that use the list provided by 304 * prl. If that is not available just use the default list. 305 */ 306 bzero(&priority_list, sizeof(priority_list)); 307 if (inpacket && inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data) 308 create_priority_list(priority_list, 309 inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].data, 310 inpacket->options[DHO_DHCP_PARAMETER_REQUEST_LIST].len); 311 else if (prl) 312 create_priority_list(priority_list, prl, prl_len); 313 else 314 create_priority_list(priority_list, NULL, 0); 315 316 /* 317 * Copy the options into the big buffer, including leading cookie and 318 * DHCP_OVERLOAD_OPTION, and DHO_END if it fits. All unused space will 319 * be set to DHO_PAD 320 */ 321 option_size = store_options(buffer, main_buffer_size, options, 322 priority_list, overload, terminate); 323 if (option_size == 0) 324 return (DHCP_FIXED_NON_UDP); 325 326 /* Copy the main buffer. */ 327 memcpy(&outpacket->options[0], buffer, main_buffer_size); 328 if (option_size <= main_buffer_size) 329 return (DHCP_FIXED_NON_UDP + option_size); 330 331 /* Copy the overflow buffers. */ 332 bufix = main_buffer_size; 333 if (overload & 1) { 334 memcpy(outpacket->file, &buffer[bufix], DHCP_FILE_LEN); 335 bufix += DHCP_FILE_LEN; 336 } 337 if (overload & 2) 338 memcpy(outpacket->sname, &buffer[bufix], DHCP_SNAME_LEN); 339 340 return (DHCP_FIXED_NON_UDP + main_buffer_size); 341 } 342 343 /* 344 * Store a <code><length><data> fragment in buffer. Return the number of 345 * characters used. Return 0 if no data could be stored. 346 */ 347 int 348 store_option_fragment(unsigned char *buffer, int buffer_size, 349 unsigned char code, int length, unsigned char *data) 350 { 351 buffer_size -= 2; /* Space for option code and option length. */ 352 353 if (buffer_size < 1) 354 return (0); 355 356 if (buffer_size > 255) 357 buffer_size = 255; 358 if (length > buffer_size) 359 length = buffer_size; 360 361 buffer[0] = code; 362 buffer[1] = length; 363 364 memcpy(&buffer[2], data, length); 365 366 return (length + 2); 367 } 368 369 /* 370 * Store all the requested options into the requested buffer. Insert the 371 * required cookie, DHO_DHCP_OPTION_OVERLOAD options and append a DHO_END if 372 * if fits. Ensure all buffer space is set to DHO_PAD if unused. 373 */ 374 int 375 store_options(unsigned char *buffer, int main_buffer_size, 376 struct tree_cache **options, unsigned char *priority_list, int overload, 377 int terminate) 378 { 379 int buflen, code, cutoff, i, incr, ix, length, optstart, overflow; 380 int second_cutoff; 381 int bufix = 0; 382 383 overload &= 3; /* Only consider valid bits. */ 384 385 cutoff = main_buffer_size; 386 second_cutoff = cutoff + ((overload & 1) ? DHCP_FILE_LEN : 0); 387 buflen = second_cutoff + ((overload & 2) ? DHCP_SNAME_LEN : 0); 388 389 memset(buffer, DHO_PAD, buflen); 390 memcpy(buffer, DHCP_OPTIONS_COOKIE, 4); 391 392 if (overload) 393 bufix = 7; /* Reserve space for DHO_DHCP_OPTION_OVERLOAD. */ 394 else 395 bufix = 4; 396 397 /* 398 * Store options in the order they appear in the priority list. 399 */ 400 for (i = 0; i < 256; i++) { 401 /* Code for next option to try to store. */ 402 code = priority_list[i]; 403 if (code == DHO_PAD || code == DHO_END) 404 continue; 405 406 if (!options[code] || !tree_evaluate(options[code])) 407 continue; 408 409 /* We should now have a constant length for the option. */ 410 length = options[code]->len; 411 412 /* Try to store the option. */ 413 optstart = bufix; 414 ix = 0; 415 while (length) { 416 incr = store_option_fragment(&buffer[bufix], 417 cutoff - bufix, code, length, 418 options[code]->value + ix); 419 420 if (incr > 0) { 421 bufix += incr; 422 length -= incr - 2; 423 ix += incr - 2; 424 continue; 425 } 426 427 /* 428 * No fragment could be stored in the space before the 429 * cutoff. Fill the unusable space with DHO_PAD and 430 * move cutoff for another attempt. 431 */ 432 memset(&buffer[bufix], DHO_PAD, cutoff - bufix); 433 bufix = cutoff; 434 if (cutoff < second_cutoff) 435 cutoff = second_cutoff; 436 else if (cutoff < buflen) 437 cutoff = buflen; 438 else 439 break; 440 } 441 442 if (length > 0) { 443 zapfrags: 444 memset(&buffer[optstart], DHO_PAD, buflen - optstart); 445 bufix = optstart; 446 } else if (terminate && dhcp_options[code].format[0] == 't') { 447 if (bufix < cutoff) 448 buffer[bufix++] = '\0'; 449 else 450 goto zapfrags; 451 } 452 } 453 454 if (bufix == (4 + (overload ? 3 : 0))) 455 /* Didn't manage to store any options. */ 456 return (0); 457 458 if (bufix < buflen) 459 buffer[bufix++] = DHO_END; 460 461 /* Fill in overload option value based on space used for options. */ 462 if (overload) { 463 overflow = bufix - main_buffer_size; 464 if (overflow > 0) { 465 buffer[4] = DHO_DHCP_OPTION_OVERLOAD; 466 buffer[5] = 1; 467 if (overload & 1) { 468 buffer[6] |= 1; 469 overflow -= DHCP_FILE_LEN; 470 } 471 if ((overload & 2) && overflow > 0) 472 buffer[6] |= 2; 473 } else { 474 /* 475 * Compact buffer to eliminate the unused 476 * DHO_DHCP_OPTION_OVERLOAD option. Some clients 477 * choke on DHO_PAD options there. 478 */ 479 memmove(&buffer[4], &buffer[7], buflen - 7); 480 bufix -= 3; 481 memset(&buffer[bufix], DHO_PAD, 3); 482 } 483 } 484 485 return (bufix); 486 } 487 488 void 489 do_packet(struct interface_info *interface, struct dhcp_packet *packet, 490 int len, unsigned int from_port, struct iaddr from, struct hardware *hfrom) 491 { 492 struct packet tp; 493 int i; 494 495 if (packet->hlen > sizeof(packet->chaddr)) { 496 note("Discarding packet with invalid hlen."); 497 return; 498 } 499 500 memset(&tp, 0, sizeof(tp)); 501 tp.raw = packet; 502 tp.packet_length = len; 503 tp.client_port = from_port; 504 tp.client_addr = from; 505 tp.interface = interface; 506 tp.haddr = hfrom; 507 508 parse_options(&tp); 509 if (tp.options_valid && 510 tp.options[DHO_DHCP_MESSAGE_TYPE].data) 511 tp.packet_type = tp.options[DHO_DHCP_MESSAGE_TYPE].data[0]; 512 if (tp.packet_type) 513 dhcp(&tp); 514 else 515 bootp(&tp); 516 517 /* Free the data associated with the options. */ 518 for (i = 0; i < 256; i++) 519 if (tp.options[i].len && tp.options[i].data) 520 free(tp.options[i].data); 521 } 522