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