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