options.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * options.c -- DHCP server option packet tools
  3. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "dhcpd.h"
  8. #include "files.h"
  9. #include "options.h"
  10. #include "common.h"
  11. /* supported options are easily added here */
  12. struct dhcp_option dhcp_options[] = {
  13. /* name[10] flags code */
  14. {"subnet", OPTION_IP | OPTION_REQ, 0x01},
  15. {"timezone", OPTION_S32, 0x02},
  16. {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
  17. {"timesvr", OPTION_IP | OPTION_LIST, 0x04},
  18. {"namesvr", OPTION_IP | OPTION_LIST, 0x05},
  19. {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
  20. {"logsvr", OPTION_IP | OPTION_LIST, 0x07},
  21. {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
  22. {"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
  23. {"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
  24. {"bootsize", OPTION_U16, 0x0d},
  25. {"domain", OPTION_STRING | OPTION_REQ, 0x0f},
  26. {"swapsvr", OPTION_IP, 0x10},
  27. {"rootpath", OPTION_STRING, 0x11},
  28. {"ipttl", OPTION_U8, 0x17},
  29. {"mtu", OPTION_U16, 0x1a},
  30. {"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
  31. {"ntpsrv", OPTION_IP | OPTION_LIST, 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. {"tftp", OPTION_STRING, 0x42},
  39. {"bootfile", OPTION_STRING, 0x43},
  40. {"", 0x00, 0x00}
  41. };
  42. /* Lengths of the different option types */
  43. int option_lengths[] = {
  44. [OPTION_IP] = 4,
  45. [OPTION_IP_PAIR] = 8,
  46. [OPTION_BOOLEAN] = 1,
  47. [OPTION_STRING] = 1,
  48. [OPTION_U8] = 1,
  49. [OPTION_U16] = 2,
  50. [OPTION_S16] = 2,
  51. [OPTION_U32] = 4,
  52. [OPTION_S32] = 4
  53. };
  54. /* get an option with bounds checking (warning, not aligned). */
  55. uint8_t *get_option(struct dhcpMessage *packet, int code)
  56. {
  57. int i, length;
  58. uint8_t *optionptr;
  59. int over = 0, done = 0, curr = OPTION_FIELD;
  60. optionptr = packet->options;
  61. i = 0;
  62. length = 308;
  63. while (!done) {
  64. if (i >= length) {
  65. LOG(LOG_WARNING, "bogus packet, option fields too long.");
  66. return NULL;
  67. }
  68. if (optionptr[i + OPT_CODE] == code) {
  69. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  70. LOG(LOG_WARNING, "bogus packet, option fields too long.");
  71. return NULL;
  72. }
  73. return optionptr + i + 2;
  74. }
  75. switch (optionptr[i + OPT_CODE]) {
  76. case DHCP_PADDING:
  77. i++;
  78. break;
  79. case DHCP_OPTION_OVER:
  80. if (i + 1 + optionptr[i + OPT_LEN] >= length) {
  81. LOG(LOG_WARNING, "bogus packet, option fields too long.");
  82. return NULL;
  83. }
  84. over = optionptr[i + 3];
  85. i += optionptr[OPT_LEN] + 2;
  86. break;
  87. case DHCP_END:
  88. if (curr == OPTION_FIELD && over & FILE_FIELD) {
  89. optionptr = packet->file;
  90. i = 0;
  91. length = 128;
  92. curr = FILE_FIELD;
  93. } else if (curr == FILE_FIELD && over & SNAME_FIELD) {
  94. optionptr = packet->sname;
  95. i = 0;
  96. length = 64;
  97. curr = SNAME_FIELD;
  98. } else done = 1;
  99. break;
  100. default:
  101. i += optionptr[OPT_LEN + i] + 2;
  102. }
  103. }
  104. return NULL;
  105. }
  106. /* return the position of the 'end' option (no bounds checking) */
  107. int end_option(uint8_t *optionptr)
  108. {
  109. int i = 0;
  110. while (optionptr[i] != DHCP_END) {
  111. if (optionptr[i] == DHCP_PADDING) i++;
  112. else i += optionptr[i + OPT_LEN] + 2;
  113. }
  114. return i;
  115. }
  116. /* add an option string to the options (an option string contains an option code,
  117. * length, then data) */
  118. int add_option_string(uint8_t *optionptr, uint8_t *string)
  119. {
  120. int end = end_option(optionptr);
  121. /* end position + string length + option code/length + end option */
  122. if (end + string[OPT_LEN] + 2 + 1 >= 308) {
  123. LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
  124. return 0;
  125. }
  126. DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
  127. memcpy(optionptr + end, string, string[OPT_LEN] + 2);
  128. optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
  129. return string[OPT_LEN] + 2;
  130. }
  131. /* add a one to four byte option to a packet */
  132. int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
  133. {
  134. char length = 0;
  135. int i;
  136. uint8_t option[2 + 4];
  137. uint8_t *u8;
  138. uint16_t *u16;
  139. uint32_t *u32;
  140. uint32_t aligned;
  141. u8 = (uint8_t *) &aligned;
  142. u16 = (uint16_t *) &aligned;
  143. u32 = &aligned;
  144. for (i = 0; dhcp_options[i].code; i++)
  145. if (dhcp_options[i].code == code) {
  146. length = option_lengths[dhcp_options[i].flags & TYPE_MASK];
  147. }
  148. if (!length) {
  149. DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
  150. return 0;
  151. }
  152. option[OPT_CODE] = code;
  153. option[OPT_LEN] = length;
  154. switch (length) {
  155. case 1: *u8 = data; break;
  156. case 2: *u16 = data; break;
  157. case 4: *u32 = data; break;
  158. }
  159. memcpy(option + 2, &aligned, length);
  160. return add_option_string(optionptr, option);
  161. }
  162. /* find option 'code' in opt_list */
  163. struct option_set *find_option(struct option_set *opt_list, char code)
  164. {
  165. while (opt_list && opt_list->data[OPT_CODE] < code)
  166. opt_list = opt_list->next;
  167. if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
  168. else return NULL;
  169. }
  170. /* add an option to the opt_list */
  171. void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
  172. {
  173. struct option_set *existing, *new, **curr;
  174. /* add it to an existing option */
  175. if ((existing = find_option(*opt_list, option->code))) {
  176. DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
  177. if (option->flags & OPTION_LIST) {
  178. if (existing->data[OPT_LEN] + length <= 255) {
  179. existing->data = realloc(existing->data,
  180. existing->data[OPT_LEN] + length + 2);
  181. memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
  182. existing->data[OPT_LEN] += length;
  183. } /* else, ignore the data, we could put this in a second option in the future */
  184. } /* else, ignore the new data */
  185. } else {
  186. DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
  187. /* make a new option */
  188. new = xmalloc(sizeof(struct option_set));
  189. new->data = xmalloc(length + 2);
  190. new->data[OPT_CODE] = option->code;
  191. new->data[OPT_LEN] = length;
  192. memcpy(new->data + 2, buffer, length);
  193. curr = opt_list;
  194. while (*curr && (*curr)->data[OPT_CODE] < option->code)
  195. curr = &(*curr)->next;
  196. new->next = *curr;
  197. *curr = new;
  198. }
  199. }