options.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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[12] 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_LIST | 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. #if ENABLE_FEATURE_RFC3397
  44. {"search", OPTION_STR1035 | OPTION_LIST | OPTION_REQ, 0x77},
  45. #endif
  46. /* MSIE's "Web Proxy Autodiscovery Protocol" support */
  47. {"wpad", OPTION_STRING, 0xfc},
  48. {"", 0x00, 0x00}
  49. };
  50. /* Lengths of the different option types */
  51. const unsigned char option_lengths[] ALIGN1 = {
  52. [OPTION_IP] = 4,
  53. [OPTION_IP_PAIR] = 8,
  54. [OPTION_BOOLEAN] = 1,
  55. [OPTION_STRING] = 1,
  56. #if ENABLE_FEATURE_RFC3397
  57. [OPTION_STR1035] = 1,
  58. #endif
  59. [OPTION_U8] = 1,
  60. [OPTION_U16] = 2,
  61. [OPTION_S16] = 2,
  62. [OPTION_U32] = 4,
  63. [OPTION_S32] = 4
  64. };
  65. /* get an option with bounds checking (warning, not aligned). */
  66. uint8_t *get_option(struct dhcpMessage *packet, int code)
  67. {
  68. int i, length;
  69. uint8_t *optionptr;
  70. int over = 0, done = 0, curr = OPTION_FIELD;
  71. optionptr = packet->options;
  72. i = 0;
  73. length = 308;
  74. while (!done) {
  75. if (i >= length) {
  76. bb_error_msg("bogus packet, option fields too long");
  77. return NULL;
  78. }
  79. if (optionptr[i + OPT_CODE] == code) {
  80. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  81. bb_error_msg("bogus packet, option fields too long");
  82. return NULL;
  83. }
  84. return optionptr + i + 2;
  85. }
  86. switch (optionptr[i + OPT_CODE]) {
  87. case DHCP_PADDING:
  88. i++;
  89. break;
  90. case DHCP_OPTION_OVER:
  91. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  92. bb_error_msg("bogus packet, option fields too long");
  93. return NULL;
  94. }
  95. over = optionptr[i + 3];
  96. i += optionptr[OPT_LEN] + 2;
  97. break;
  98. case DHCP_END:
  99. if (curr == OPTION_FIELD && over & FILE_FIELD) {
  100. optionptr = packet->file;
  101. i = 0;
  102. length = 128;
  103. curr = FILE_FIELD;
  104. } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
  105. optionptr = packet->sname;
  106. i = 0;
  107. length = 64;
  108. curr = SNAME_FIELD;
  109. } else done = 1;
  110. break;
  111. default:
  112. i += optionptr[OPT_LEN + i] + 2;
  113. }
  114. }
  115. return NULL;
  116. }
  117. /* return the position of the 'end' option (no bounds checking) */
  118. int end_option(uint8_t *optionptr)
  119. {
  120. int i = 0;
  121. while (optionptr[i] != DHCP_END) {
  122. if (optionptr[i] == DHCP_PADDING) i++;
  123. else i += optionptr[i + OPT_LEN] + 2;
  124. }
  125. return i;
  126. }
  127. /* add an option string to the options (an option string contains an option code,
  128. * length, then data) */
  129. int add_option_string(uint8_t *optionptr, uint8_t *string)
  130. {
  131. int end = end_option(optionptr);
  132. /* end position + string length + option code/length + end option */
  133. if (end + string[OPT_LEN] + 2 + 1 >= 308) {
  134. bb_error_msg("option 0x%02x did not fit into the packet",
  135. string[OPT_CODE]);
  136. return 0;
  137. }
  138. DEBUG("adding option 0x%02x", string[OPT_CODE]);
  139. memcpy(optionptr + end, string, string[OPT_LEN] + 2);
  140. optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
  141. return string[OPT_LEN] + 2;
  142. }
  143. /* add a one to four byte option to a packet */
  144. int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
  145. {
  146. const struct dhcp_option *dh;
  147. for (dh = dhcp_options; dh->code; dh++) {
  148. if (dh->code == code) {
  149. uint8_t option[6], len;
  150. option[OPT_CODE] = code;
  151. len = option_lengths[dh->flags & TYPE_MASK];
  152. option[OPT_LEN] = len;
  153. if (BB_BIG_ENDIAN) data <<= 8 * (4 - len);
  154. /* This memcpy is for broken processors which can't
  155. * handle a simple unaligned 32-bit assignment */
  156. memcpy(&option[OPT_DATA], &data, 4);
  157. return add_option_string(optionptr, option);
  158. }
  159. }
  160. bb_error_msg("cannot add option 0x%02x", code);
  161. return 0;
  162. }