"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * options.c -- DHCP server option packet tools |
| 4 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 5 | */ |
| 6 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | #include "common.h" |
| 8 | #include "dhcpd.h" |
| 9 | #include "options.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | |
| 11 | |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 12 | /* Supported options are easily added here */ |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 13 | const struct dhcp_option dhcp_options[] = { |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 14 | /* flags code */ |
| 15 | { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ |
| 16 | { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ |
| 17 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ |
| 18 | { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ |
| 19 | { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ |
| 20 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ |
| 21 | { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ |
| 22 | { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ |
| 23 | { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ |
| 24 | { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ |
| 25 | { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */ |
| 26 | { OPTION_STRING | OPTION_LIST | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ |
| 27 | { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */ |
| 28 | { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */ |
| 29 | { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ |
| 30 | { OPTION_U16 , 0x1a }, /* DHCP_MTU */ |
| 31 | { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ |
| 32 | { OPTION_STRING , 0x28 }, /* nisdomain */ |
| 33 | { OPTION_IP | OPTION_LIST , 0x29 }, /* nissrv */ |
| 34 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ |
| 35 | { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ |
| 36 | { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ |
| 37 | { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ |
| 38 | { OPTION_U8 , 0x35 }, /* dhcptype */ |
| 39 | { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ |
| 40 | { OPTION_STRING , 0x38 }, /* DHCP_MESSAGE */ |
| 41 | { OPTION_STRING , 0x3C }, /* DHCP_VENDOR */ |
| 42 | { OPTION_STRING , 0x3D }, /* DHCP_CLIENT_ID */ |
| 43 | { OPTION_STRING , 0x42 }, /* tftp */ |
| 44 | { OPTION_STRING , 0x43 }, /* bootfile */ |
| 45 | { OPTION_STRING , 0x4D }, /* userclass */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame^] | 46 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 47 | { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* search */ |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 48 | #endif |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 49 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 50 | { OPTION_STRING , 0xfc }, /* wpad */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 51 | |
| 52 | /* Options below have no match in dhcp_option_strings[], |
| 53 | * are not passed to dhcpc scripts, and cannot be specified |
| 54 | * with "option XXX YYY" syntax in dhcpd config file. */ |
| 55 | |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 56 | { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 57 | { } /* zeroed terminating entry */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 60 | /* Used for converting options from incoming packets to env variables |
| 61 | * for udhcpc stript */ |
| 62 | /* Must match dhcp_options[] order */ |
| 63 | const char dhcp_option_strings[] ALIGN1 = |
| 64 | "subnet" "\0" /* DHCP_SUBNET */ |
| 65 | "timezone" "\0" /* DHCP_TIME_OFFSET */ |
| 66 | "router" "\0" /* DHCP_ROUTER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 67 | "timesrv" "\0" /* DHCP_TIME_SERVER */ |
| 68 | "namesrv" "\0" /* DHCP_NAME_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 69 | "dns" "\0" /* DHCP_DNS_SERVER */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 70 | "logsrv" "\0" /* DHCP_LOG_SERVER */ |
| 71 | "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ |
| 72 | "lprsrv" "\0" /* DHCP_LPR_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 73 | "hostname" "\0" /* DHCP_HOST_NAME */ |
| 74 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ |
| 75 | "domain" "\0" /* DHCP_DOMAIN_NAME */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 76 | "swapsrv" "\0" /* DHCP_SWAP_SERVER */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 77 | "rootpath" "\0" /* DHCP_ROOT_PATH */ |
| 78 | "ipttl" "\0" /* DHCP_IP_TTL */ |
| 79 | "mtu" "\0" /* DHCP_MTU */ |
| 80 | "broadcast" "\0" /* DHCP_BROADCAST */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 81 | "nisdomain" "\0" /* */ |
| 82 | "nissrv" "\0" /* */ |
| 83 | "ntpsrv" "\0" /* DHCP_NTP_SERVER */ |
| 84 | "wins" "\0" /* DHCP_WINS_SERVER */ |
| 85 | "requestip" "\0" /* DHCP_REQUESTED_IP */ |
| 86 | "lease" "\0" /* DHCP_LEASE_TIME */ |
| 87 | "dhcptype" "\0" /* */ |
| 88 | "serverid" "\0" /* DHCP_SERVER_ID */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 89 | "message" "\0" /* DHCP_MESSAGE */ |
| 90 | "vendorclass" "\0" /* DHCP_VENDOR */ |
| 91 | "clientid" "\0" /* DHCP_CLIENT_ID */ |
| 92 | "tftp" "\0" |
| 93 | "bootfile" "\0" |
| 94 | "userclass" "\0" |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame^] | 95 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 96 | "search" "\0" |
| 97 | #endif |
| 98 | /* MSIE's "Web Proxy Autodiscovery Protocol" support */ |
| 99 | "wpad" "\0" |
| 100 | ; |
| 101 | |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 102 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | /* Lengths of the different option types */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 104 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 105 | [OPTION_IP] = 4, |
| 106 | [OPTION_IP_PAIR] = 8, |
| 107 | [OPTION_BOOLEAN] = 1, |
| 108 | [OPTION_STRING] = 1, |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame^] | 109 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 110 | [OPTION_STR1035] = 1, |
| 111 | #endif |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 112 | [OPTION_U8] = 1, |
| 113 | [OPTION_U16] = 2, |
| 114 | [OPTION_S16] = 2, |
| 115 | [OPTION_U32] = 4, |
| 116 | [OPTION_S32] = 4 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | |
| 120 | /* get an option with bounds checking (warning, not aligned). */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 121 | uint8_t* FAST_FUNC get_option(struct dhcpMessage *packet, int code) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 122 | { |
| 123 | int i, length; |
| 124 | uint8_t *optionptr; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 125 | int over = 0; |
| 126 | int curr = OPTION_FIELD; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 127 | |
| 128 | optionptr = packet->options; |
| 129 | i = 0; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 130 | length = sizeof(packet->options); |
| 131 | while (1) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 132 | if (i >= length) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 133 | bb_error_msg("bogus packet, option fields too long"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | if (optionptr[i + OPT_CODE] == code) { |
| 137 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 138 | bb_error_msg("bogus packet, option fields too long"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 139 | return NULL; |
| 140 | } |
| 141 | return optionptr + i + 2; |
| 142 | } |
| 143 | switch (optionptr[i + OPT_CODE]) { |
| 144 | case DHCP_PADDING: |
| 145 | i++; |
| 146 | break; |
| 147 | case DHCP_OPTION_OVER: |
| 148 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 149 | bb_error_msg("bogus packet, option fields too long"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
| 152 | over = optionptr[i + 3]; |
| 153 | i += optionptr[OPT_LEN] + 2; |
| 154 | break; |
| 155 | case DHCP_END: |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 156 | if (curr == OPTION_FIELD && (over & FILE_FIELD)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 157 | optionptr = packet->file; |
| 158 | i = 0; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 159 | length = sizeof(packet->file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 160 | curr = FILE_FIELD; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 161 | } else if (curr == FILE_FIELD && (over & SNAME_FIELD)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 162 | optionptr = packet->sname; |
| 163 | i = 0; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 164 | length = sizeof(packet->sname); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | curr = SNAME_FIELD; |
Denis Vlasenko | 6884f66 | 2007-11-23 00:08:54 +0000 | [diff] [blame] | 166 | } else |
| 167 | return NULL; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 168 | break; |
| 169 | default: |
| 170 | i += optionptr[OPT_LEN + i] + 2; |
| 171 | } |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* return the position of the 'end' option (no bounds checking) */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 178 | int FAST_FUNC end_option(uint8_t *optionptr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 179 | { |
| 180 | int i = 0; |
| 181 | |
| 182 | while (optionptr[i] != DHCP_END) { |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 183 | if (optionptr[i] == DHCP_PADDING) |
| 184 | i++; |
| 185 | else |
| 186 | i += optionptr[i + OPT_LEN] + 2; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 187 | } |
| 188 | return i; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* add an option string to the options (an option string contains an option code, |
| 193 | * length, then data) */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 194 | int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 195 | { |
| 196 | int end = end_option(optionptr); |
| 197 | |
| 198 | /* end position + string length + option code/length + end option */ |
Denis Vlasenko | 72e7604 | 2007-11-25 03:15:24 +0000 | [diff] [blame] | 199 | if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 200 | bb_error_msg("option 0x%02x did not fit into the packet", |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 201 | string[OPT_CODE]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 204 | DEBUG("adding option 0x%02x", string[OPT_CODE]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 206 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
| 207 | return string[OPT_LEN] + 2; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* add a one to four byte option to a packet */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 212 | int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 213 | { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 214 | const struct dhcp_option *dh; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 216 | for (dh = dhcp_options; dh->code; dh++) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 217 | if (dh->code == code) { |
| 218 | uint8_t option[6], len; |
| 219 | |
| 220 | option[OPT_CODE] = code; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 221 | len = dhcp_option_lengths[dh->flags & TYPE_MASK]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 222 | option[OPT_LEN] = len; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 223 | if (BB_BIG_ENDIAN) |
| 224 | data <<= 8 * (4 - len); |
| 225 | /* This memcpy is for processors which can't |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 226 | * handle a simple unaligned 32-bit assignment */ |
| 227 | memcpy(&option[OPT_DATA], &data, 4); |
| 228 | return add_option_string(optionptr, option); |
| 229 | } |
| 230 | } |
| 231 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 232 | bb_error_msg("cannot add option 0x%02x", code); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } |