options.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * options.c -- DHCP server option packet tools
  4. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  5. */
  6. #include "common.h"
  7. #include "dhcpd.h"
  8. #include "options.h"
  9. /* supported options are easily added here */
  10. const struct dhcp_option dhcp_options[] = {
  11. /* name[10] flags code */
  12. {"subnet", OPTION_IP | OPTION_REQ, 0x01},
  13. {"timezone", OPTION_S32, 0x02},
  14. {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
  15. {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
  16. {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
  17. {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
  18. {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
  19. {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
  20. {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
  21. {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
  22. {"bootsize", OPTION_U16, 0x0d},
  23. {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
  24. {"swapsvr", OPTION_IP, 0x10},
  25. {"rootpath", OPTION_STRING, 0x11},
  26. {"ipttl", OPTION_U8, 0x17},
  27. {"mtu", OPTION_U16, 0x1a},
  28. {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
  29. {"nisdomain", OPTION_STRING | OPTION_REQ, 0x28},
  30. {"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29},
  31. {"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a},
  32. {"wins", OPTION_IP | OPTION_LIST, 0x2c},
  33. {"requestip", OPTION_IP, 0x32},
  34. {"lease", OPTION_U32, 0x33},
  35. {"dhcptype", OPTION_U8, 0x35},
  36. {"serverid", OPTION_IP, 0x36},
  37. {"message", OPTION_STRING, 0x38},
  38. {"vendorclass", OPTION_STRING, 0x3C},
  39. {"clientid", OPTION_STRING, 0x3D},
  40. {"tftp", OPTION_STRING, 0x42},
  41. {"bootfile", OPTION_STRING, 0x43},
  42. {"userclass", OPTION_STRING, 0x4D},
  43. /* MSIE's "Web Proxy Autodiscovery Protocol" support */
  44. {"wpad", OPTION_STRING, 0xfc},
  45. {"", 0x00, 0x00}
  46. };
  47. /* Lengths of the different option types */
  48. const unsigned char option_lengths[] = {
  49. [OPTION_IP] = 4,
  50. [OPTION_IP_PAIR] = 8,
  51. [OPTION_BOOLEAN] = 1,
  52. [OPTION_STRING] = 1,
  53. [OPTION_U8] = 1,
  54. [OPTION_U16] = 2,
  55. [OPTION_S16] = 2,
  56. [OPTION_U32] = 4,
  57. [OPTION_S32] = 4
  58. };
  59. /* get an option with bounds checking (warning, not aligned). */
  60. uint8_t *get_option(struct dhcpMessage *packet, int code)
  61. {
  62. int i, length;
  63. uint8_t *optionptr;
  64. int over = 0, done = 0, curr = OPTION_FIELD;
  65. optionptr = packet->options;
  66. i = 0;
  67. length = 308;
  68. while (!done) {
  69. if (i >= length) {
  70. bb_error_msg("bogus packet, option fields too long");
  71. return NULL;
  72. }
  73. if (optionptr[i + OPT_CODE] == code) {
  74. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  75. bb_error_msg("bogus packet, option fields too long");
  76. return NULL;
  77. }
  78. return optionptr + i + 2;
  79. }
  80. switch (optionptr[i + OPT_CODE]) {
  81. case DHCP_PADDING:
  82. i++;
  83. break;
  84. case DHCP_OPTION_OVER:
  85. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  86. bb_error_msg("bogus packet, option fields too long");
  87. return NULL;
  88. }
  89. over = optionptr[i + 3];
  90. i += optionptr[OPT_LEN] + 2;
  91. break;
  92. case DHCP_END:
  93. if (curr == OPTION_FIELD && over & FILE_FIELD) {
  94. optionptr = packet->file;
  95. i = 0;
  96. length = 128;
  97. curr = FILE_FIELD;
  98. } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
  99. optionptr = packet->sname;
  100. i = 0;
  101. length = 64;
  102. curr = SNAME_FIELD;
  103. } else done = 1;
  104. break;
  105. default:
  106. i += optionptr[OPT_LEN + i] + 2;
  107. }
  108. }
  109. return NULL;
  110. }
  111. /* return the position of the 'end' option (no bounds checking) */
  112. int end_option(uint8_t *optionptr)
  113. {
  114. int i = 0;
  115. while (optionptr[i] != DHCP_END) {
  116. if (optionptr[i] == DHCP_PADDING) i++;
  117. else i += optionptr[i + OPT_LEN] + 2;
  118. }
  119. return i;
  120. }
  121. /* add an option string to the options (an option string contains an option code,
  122. * length, then data) */
  123. int add_option_string(uint8_t *optionptr, uint8_t *string)
  124. {
  125. int end = end_option(optionptr);
  126. /* end position + string length + option code/length + end option */
  127. if (end + string[OPT_LEN] + 2 + 1 >= 308) {
  128. bb_error_msg("option 0x%02x did not fit into the packet",
  129. string[OPT_CODE]);
  130. return 0;
  131. }
  132. DEBUG("adding option 0x%02x", string[OPT_CODE]);
  133. memcpy(optionptr + end, string, string[OPT_LEN] + 2);
  134. optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
  135. return string[OPT_LEN] + 2;
  136. }
  137. /* add a one to four byte option to a packet */
  138. int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
  139. {
  140. const struct dhcp_option *dh;
  141. for (dh = dhcp_options; dh->code; dh++) {
  142. if (dh->code == code) {
  143. uint8_t option[6], len;
  144. option[OPT_CODE] = code;
  145. len = option_lengths[dh->flags & TYPE_MASK];
  146. option[OPT_LEN] = len;
  147. if (BB_BIG_ENDIAN) data <<= 8 * (4 - len);
  148. /* This memcpy is for broken processors which can't
  149. * handle a simple unaligned 32-bit assignment */
  150. memcpy(&option[OPT_DATA], &data, 4);
  151. return add_option_string(optionptr, option);
  152. }
  153. }
  154. bb_error_msg("cannot add option 0x%02x", code);
  155. return 0;
  156. }