Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 6 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 8 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 9 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 10 | unsigned dhcp_verbose; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 11 | #endif |
| 12 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 13 | const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = { |
| 14 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 15 | }; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 16 | |
| 17 | /* Supported options are easily added here. |
| 18 | * See RFC2132 for more options. |
| 19 | * OPTION_REQ: these options are requested by udhcpc (unless -o). |
| 20 | */ |
| 21 | const struct dhcp_option dhcp_options[] = { |
| 22 | /* flags code */ |
| 23 | { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ |
| 24 | { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ |
| 25 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ |
| 26 | // { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ |
| 27 | // { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ |
| 28 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ |
| 29 | // { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ |
| 30 | // { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ |
| 31 | { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ |
| 32 | { OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ |
| 33 | { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */ |
| 34 | { OPTION_STRING | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ |
| 35 | { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */ |
| 36 | { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */ |
| 37 | { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ |
| 38 | { OPTION_U16 , 0x1a }, /* DHCP_MTU */ |
| 39 | { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ |
| 40 | { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */ |
| 41 | { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */ |
| 42 | { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ |
| 43 | { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ |
| 44 | { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ |
| 45 | { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ |
| 46 | { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */ |
| 47 | //TODO: must be combined with 'sname' and 'file' handling: |
| 48 | { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */ |
| 49 | { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */ |
| 50 | //TODO: not a string, but a set of LASCII strings: |
| 51 | // { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */ |
| 52 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 53 | { OPTION_DNS_STRING | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */ |
| 54 | { OPTION_SIP_SERVERS , 0x78 }, /* DHCP_SIP_SERVERS */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 55 | #endif |
| 56 | { OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */ |
| 57 | { OPTION_STRING , 0xfc }, /* DHCP_WPAD */ |
| 58 | |
| 59 | /* Options below have no match in dhcp_option_strings[], |
| 60 | * are not passed to dhcpc scripts, and cannot be specified |
| 61 | * with "option XXX YYY" syntax in dhcpd config file. |
| 62 | * These entries are only used internally by udhcp[cd] |
| 63 | * to correctly encode options into packets. |
| 64 | */ |
| 65 | |
| 66 | { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ |
| 67 | { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ |
| 68 | { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ |
| 69 | { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ |
| 70 | //FIXME: handling of this option is not exactly correct: |
| 71 | { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ |
| 72 | { 0, 0 } /* zeroed terminating entry */ |
| 73 | }; |
| 74 | |
| 75 | /* Used for converting options from incoming packets to env variables |
| 76 | * for udhcpc stript, and for setting options for udhcpd via |
| 77 | * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file. |
| 78 | */ |
| 79 | /* Must match dhcp_options[] order */ |
| 80 | const char dhcp_option_strings[] ALIGN1 = |
| 81 | "subnet" "\0" /* DHCP_SUBNET */ |
| 82 | "timezone" "\0" /* DHCP_TIME_OFFSET */ |
| 83 | "router" "\0" /* DHCP_ROUTER */ |
| 84 | // "timesrv" "\0" /* DHCP_TIME_SERVER */ |
| 85 | // "namesrv" "\0" /* DHCP_NAME_SERVER */ |
| 86 | "dns" "\0" /* DHCP_DNS_SERVER */ |
| 87 | // "logsrv" "\0" /* DHCP_LOG_SERVER */ |
| 88 | // "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ |
| 89 | "lprsrv" "\0" /* DHCP_LPR_SERVER */ |
| 90 | "hostname" "\0" /* DHCP_HOST_NAME */ |
| 91 | "bootsize" "\0" /* DHCP_BOOT_SIZE */ |
| 92 | "domain" "\0" /* DHCP_DOMAIN_NAME */ |
| 93 | "swapsrv" "\0" /* DHCP_SWAP_SERVER */ |
| 94 | "rootpath" "\0" /* DHCP_ROOT_PATH */ |
| 95 | "ipttl" "\0" /* DHCP_IP_TTL */ |
| 96 | "mtu" "\0" /* DHCP_MTU */ |
| 97 | "broadcast" "\0" /* DHCP_BROADCAST */ |
| 98 | "nisdomain" "\0" /* DHCP_NIS_DOMAIN */ |
| 99 | "nissrv" "\0" /* DHCP_NIS_SERVER */ |
| 100 | "ntpsrv" "\0" /* DHCP_NTP_SERVER */ |
| 101 | "wins" "\0" /* DHCP_WINS_SERVER */ |
| 102 | "lease" "\0" /* DHCP_LEASE_TIME */ |
| 103 | "serverid" "\0" /* DHCP_SERVER_ID */ |
| 104 | "message" "\0" /* DHCP_ERR_MESSAGE */ |
| 105 | "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */ |
| 106 | "bootfile" "\0" /* DHCP_BOOT_FILE */ |
| 107 | // "userclass" "\0" /* DHCP_USER_CLASS */ |
| 108 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 109 | "search" "\0" /* DHCP_DOMAIN_SEARCH */ |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 110 | // doesn't work in udhcpd.conf since OPTION_SIP_SERVERS |
| 111 | // is not handled yet by "string->option" conversion code: |
| 112 | "sipservers" "\0" /* DHCP_SIP_SERVERS */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 113 | #endif |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 114 | // doesn't work in udhcpd.conf since OPTION_STATIC_ROUTES |
| 115 | // is not handled yet by "string->option" conversion code: |
| 116 | "staticroutes" "\0"/* DHCP_STATIC_ROUTES */ |
| 117 | "wpad" "\0" /* DHCP_WPAD */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 118 | ; |
| 119 | |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 120 | /* Lengths of the option types in binary form. |
| 121 | * Used by: |
| 122 | * udhcp_str2optset: to determine how many bytes to allocate. |
| 123 | * xmalloc_optname_optval: to estimate string length |
| 124 | * from binary option length: (option[LEN] / dhcp_option_lengths[opt_type]) |
| 125 | * is the number of elements, multiply in by one element's string width |
| 126 | * (len_of_option_as_string[opt_type]) and you know how wide string you need. |
| 127 | */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 128 | const uint8_t dhcp_option_lengths[] ALIGN1 = { |
| 129 | [OPTION_IP] = 4, |
| 130 | [OPTION_IP_PAIR] = 8, |
| 131 | // [OPTION_BOOLEAN] = 1, |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 132 | [OPTION_STRING] = 1, /* ignored by udhcp_str2optset */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 133 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 134 | [OPTION_DNS_STRING] = 1, /* ignored by both udhcp_str2optset and xmalloc_optname_optval */ |
| 135 | [OPTION_SIP_SERVERS] = 1, |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 136 | #endif |
| 137 | [OPTION_U8] = 1, |
| 138 | [OPTION_U16] = 2, |
| 139 | // [OPTION_S16] = 2, |
| 140 | [OPTION_U32] = 4, |
| 141 | [OPTION_S32] = 4, |
| 142 | /* Just like OPTION_STRING, we use minimum length here */ |
| 143 | [OPTION_STATIC_ROUTES] = 5, |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
| 148 | static void log_option(const char *pfx, const uint8_t *opt) |
| 149 | { |
| 150 | if (dhcp_verbose >= 2) { |
| 151 | char buf[256 * 2 + 2]; |
| 152 | *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; |
| 153 | bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); |
| 154 | } |
| 155 | } |
| 156 | #else |
| 157 | # define log_option(pfx, opt) ((void)0) |
| 158 | #endif |
| 159 | |
Denys Vlasenko | 9107b63 | 2010-03-27 23:23:41 +0100 | [diff] [blame] | 160 | unsigned FAST_FUNC udhcp_option_idx(const char *name) |
| 161 | { |
| 162 | int n = index_in_strings(dhcp_option_strings, name); |
| 163 | if (n >= 0) |
| 164 | return n; |
| 165 | |
| 166 | { |
| 167 | char buf[sizeof(dhcp_option_strings)]; |
| 168 | char *d = buf; |
| 169 | const char *s = dhcp_option_strings; |
| 170 | while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) { |
| 171 | *d++ = (*s == '\0' ? ' ' : *s); |
| 172 | s++; |
| 173 | } |
| 174 | *d = '\0'; |
| 175 | bb_error_msg_and_die("unknown option '%s', known options: %s", name, buf); |
| 176 | } |
| 177 | } |
| 178 | |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 179 | /* get an option with bounds checking (warning, result is not aligned). */ |
| 180 | uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code) |
| 181 | { |
| 182 | uint8_t *optionptr; |
| 183 | int len; |
| 184 | int rem; |
| 185 | int overload = 0; |
| 186 | enum { |
| 187 | FILE_FIELD101 = FILE_FIELD * 0x101, |
| 188 | SNAME_FIELD101 = SNAME_FIELD * 0x101, |
| 189 | }; |
| 190 | |
| 191 | /* option bytes: [code][len][data1][data2]..[dataLEN] */ |
| 192 | optionptr = packet->options; |
| 193 | rem = sizeof(packet->options); |
| 194 | while (1) { |
| 195 | if (rem <= 0) { |
| 196 | bb_error_msg("bad packet, malformed option field"); |
| 197 | return NULL; |
| 198 | } |
| 199 | if (optionptr[OPT_CODE] == DHCP_PADDING) { |
| 200 | rem--; |
| 201 | optionptr++; |
| 202 | continue; |
| 203 | } |
| 204 | if (optionptr[OPT_CODE] == DHCP_END) { |
| 205 | if ((overload & FILE_FIELD101) == FILE_FIELD) { |
| 206 | /* can use packet->file, and didn't look at it yet */ |
| 207 | overload |= FILE_FIELD101; /* "we looked at it" */ |
| 208 | optionptr = packet->file; |
| 209 | rem = sizeof(packet->file); |
| 210 | continue; |
| 211 | } |
| 212 | if ((overload & SNAME_FIELD101) == SNAME_FIELD) { |
| 213 | /* can use packet->sname, and didn't look at it yet */ |
| 214 | overload |= SNAME_FIELD101; /* "we looked at it" */ |
| 215 | optionptr = packet->sname; |
| 216 | rem = sizeof(packet->sname); |
| 217 | continue; |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | len = 2 + optionptr[OPT_LEN]; |
| 222 | rem -= len; |
| 223 | if (rem < 0) |
| 224 | continue; /* complain and return NULL */ |
| 225 | |
| 226 | if (optionptr[OPT_CODE] == code) { |
| 227 | log_option("Option found", optionptr); |
| 228 | return optionptr + OPT_DATA; |
| 229 | } |
| 230 | |
| 231 | if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) { |
| 232 | overload |= optionptr[OPT_DATA]; |
| 233 | /* fall through */ |
| 234 | } |
| 235 | optionptr += len; |
| 236 | } |
| 237 | |
| 238 | /* log3 because udhcpc uses it a lot - very noisy */ |
| 239 | log3("Option 0x%02x not found", code); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | /* return the position of the 'end' option (no bounds checking) */ |
| 244 | int FAST_FUNC udhcp_end_option(uint8_t *optionptr) |
| 245 | { |
| 246 | int i = 0; |
| 247 | |
| 248 | while (optionptr[i] != DHCP_END) { |
| 249 | if (optionptr[i] != DHCP_PADDING) |
| 250 | i += optionptr[i + OPT_LEN] + OPT_DATA-1; |
| 251 | i++; |
| 252 | } |
| 253 | return i; |
| 254 | } |
| 255 | |
| 256 | /* Add an option (supplied in binary form) to the options. |
| 257 | * Option format: [code][len][data1][data2]..[dataLEN] |
| 258 | */ |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 259 | void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 260 | { |
| 261 | unsigned len; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 262 | uint8_t *optionptr = packet->options; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 263 | unsigned end = udhcp_end_option(optionptr); |
| 264 | |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 265 | len = OPT_DATA + addopt[OPT_LEN]; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 266 | /* end position + (option code/length + addopt length) + end option */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 267 | if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) { |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 268 | //TODO: learn how to use overflow option if we exhaust packet->options[] |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 269 | bb_error_msg("option 0x%02x did not fit into the packet", |
| 270 | addopt[OPT_CODE]); |
| 271 | return; |
| 272 | } |
| 273 | log_option("Adding option", addopt); |
| 274 | memcpy(optionptr + end, addopt, len); |
| 275 | optionptr[end + len] = DHCP_END; |
| 276 | } |
| 277 | |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 278 | /* Add an one to four byte option to a packet */ |
| 279 | void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 280 | { |
| 281 | const struct dhcp_option *dh; |
| 282 | |
| 283 | for (dh = dhcp_options; dh->code; dh++) { |
| 284 | if (dh->code == code) { |
| 285 | uint8_t option[6], len; |
| 286 | |
| 287 | option[OPT_CODE] = code; |
| 288 | len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK]; |
| 289 | option[OPT_LEN] = len; |
| 290 | if (BB_BIG_ENDIAN) |
| 291 | data <<= 8 * (4 - len); |
| 292 | /* Assignment is unaligned! */ |
| 293 | move_to_unaligned32(&option[OPT_DATA], data); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 294 | udhcp_add_binary_option(packet, option); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | bb_error_msg("can't add option 0x%02x", code); |
| 300 | } |
| 301 | |
| 302 | /* Find option 'code' in opt_list */ |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 303 | struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code) |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 304 | { |
| 305 | while (opt_list && opt_list->data[OPT_CODE] < code) |
| 306 | opt_list = opt_list->next; |
| 307 | |
| 308 | if (opt_list && opt_list->data[OPT_CODE] == code) |
| 309 | return opt_list; |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | /* Parse string to IP in network order */ |
| 314 | int FAST_FUNC udhcp_str2nip(const char *str, void *arg) |
| 315 | { |
| 316 | len_and_sockaddr *lsa; |
| 317 | |
| 318 | lsa = host_and_af2sockaddr(str, 0, AF_INET); |
| 319 | if (!lsa) |
| 320 | return 0; |
| 321 | *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr; |
| 322 | free(lsa); |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | /* udhcp_str2optset: |
| 327 | * Parse string option representation to binary form |
| 328 | * and add it to opt_list |
| 329 | */ |
| 330 | /* helper: add an option to the opt_list */ |
| 331 | static NOINLINE void attach_option( |
| 332 | struct option_set **opt_list, |
| 333 | const struct dhcp_option *option, |
| 334 | char *buffer, |
| 335 | int length) |
| 336 | { |
| 337 | struct option_set *existing, *new, **curr; |
| 338 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 339 | char *allocated = NULL; |
| 340 | #endif |
| 341 | |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 342 | existing = udhcp_find_option(*opt_list, option->code); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 343 | if (!existing) { |
| 344 | log2("Attaching option %02x to list", option->code); |
| 345 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 346 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 347 | /* reuse buffer and length for RFC1035-formatted string */ |
| 348 | allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length); |
| 349 | } |
| 350 | #endif |
| 351 | /* make a new option */ |
| 352 | new = xmalloc(sizeof(*new)); |
| 353 | new->data = xmalloc(length + OPT_DATA); |
| 354 | new->data[OPT_CODE] = option->code; |
| 355 | new->data[OPT_LEN] = length; |
| 356 | memcpy(new->data + OPT_DATA, buffer, length); |
| 357 | |
| 358 | curr = opt_list; |
| 359 | while (*curr && (*curr)->data[OPT_CODE] < option->code) |
| 360 | curr = &(*curr)->next; |
| 361 | |
| 362 | new->next = *curr; |
| 363 | *curr = new; |
| 364 | goto ret; |
| 365 | } |
| 366 | |
| 367 | if (option->flags & OPTION_LIST) { |
| 368 | unsigned old_len; |
| 369 | |
| 370 | /* add it to an existing option */ |
| 371 | log1("Attaching option %02x to existing member of list", option->code); |
| 372 | old_len = existing->data[OPT_LEN]; |
| 373 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 374 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 375 | /* reuse buffer and length for RFC1035-formatted string */ |
| 376 | allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length); |
| 377 | } |
| 378 | #endif |
| 379 | if (old_len + length < 255) { |
| 380 | /* actually 255 is ok too, but adding a space can overlow it */ |
| 381 | |
| 382 | existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length); |
| 383 | if ((option->flags & OPTION_TYPE_MASK) == OPTION_STRING) { |
| 384 | /* add space separator between STRING options in a list */ |
| 385 | existing->data[OPT_DATA + old_len] = ' '; |
| 386 | old_len++; |
| 387 | } |
| 388 | memcpy(existing->data + OPT_DATA + old_len, buffer, length); |
| 389 | existing->data[OPT_LEN] = old_len + length; |
| 390 | } /* else, ignore the data, we could put this in a second option in the future */ |
| 391 | } /* else, ignore the new data */ |
| 392 | |
| 393 | ret: ; |
| 394 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 395 | free(allocated); |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg) |
| 400 | { |
| 401 | struct option_set **opt_list = arg; |
| 402 | char *opt, *val, *endptr; |
| 403 | char *str; |
| 404 | const struct dhcp_option *option; |
Denys Vlasenko | 9107b63 | 2010-03-27 23:23:41 +0100 | [diff] [blame] | 405 | int retval, length; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 406 | char buffer[8] ALIGNED(4); |
| 407 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 408 | uint32_t *result_u32 = (uint32_t *) buffer; |
| 409 | |
| 410 | /* Cheat, the only *const* str possible is "" */ |
| 411 | str = (char *) const_str; |
| 412 | opt = strtok(str, " \t="); |
| 413 | if (!opt) |
| 414 | return 0; |
| 415 | |
Denys Vlasenko | 9107b63 | 2010-03-27 23:23:41 +0100 | [diff] [blame] | 416 | option = &dhcp_options[udhcp_option_idx(opt)]; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 417 | |
| 418 | retval = 0; |
| 419 | do { |
| 420 | val = strtok(NULL, ", \t"); |
| 421 | if (!val) |
| 422 | break; |
| 423 | length = dhcp_option_lengths[option->flags & OPTION_TYPE_MASK]; |
| 424 | retval = 0; |
| 425 | opt = buffer; /* new meaning for variable opt */ |
| 426 | switch (option->flags & OPTION_TYPE_MASK) { |
| 427 | case OPTION_IP: |
| 428 | retval = udhcp_str2nip(val, buffer); |
| 429 | break; |
| 430 | case OPTION_IP_PAIR: |
| 431 | retval = udhcp_str2nip(val, buffer); |
| 432 | val = strtok(NULL, ", \t/-"); |
| 433 | if (!val) |
| 434 | retval = 0; |
| 435 | if (retval) |
| 436 | retval = udhcp_str2nip(val, buffer + 4); |
| 437 | break; |
| 438 | case OPTION_STRING: |
| 439 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame^] | 440 | case OPTION_DNS_STRING: |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 441 | #endif |
| 442 | length = strnlen(val, 254); |
| 443 | if (length > 0) { |
| 444 | opt = val; |
| 445 | retval = 1; |
| 446 | } |
| 447 | break; |
| 448 | // case OPTION_BOOLEAN: { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 449 | // static const char no_yes[] ALIGN1 = "no\0yes\0"; |
| 450 | // buffer[0] = retval = index_in_strings(no_yes, val); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 451 | // retval++; /* 0 - bad; 1: "no" 2: "yes" */ |
| 452 | // break; |
| 453 | // } |
| 454 | case OPTION_U8: |
| 455 | buffer[0] = strtoul(val, &endptr, 0); |
| 456 | retval = (endptr[0] == '\0'); |
| 457 | break; |
| 458 | /* htonX are macros in older libc's, using temp var |
| 459 | * in code below for safety */ |
| 460 | /* TODO: use bb_strtoX? */ |
| 461 | case OPTION_U16: { |
| 462 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 463 | *result_u16 = htons(tmp); |
| 464 | retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/); |
| 465 | break; |
| 466 | } |
| 467 | // case OPTION_S16: { |
| 468 | // long tmp = strtol(val, &endptr, 0); |
| 469 | // *result_u16 = htons(tmp); |
| 470 | // retval = (endptr[0] == '\0'); |
| 471 | // break; |
| 472 | // } |
| 473 | case OPTION_U32: { |
| 474 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 475 | *result_u32 = htonl(tmp); |
| 476 | retval = (endptr[0] == '\0'); |
| 477 | break; |
| 478 | } |
| 479 | case OPTION_S32: { |
| 480 | long tmp = strtol(val, &endptr, 0); |
| 481 | *result_u32 = htonl(tmp); |
| 482 | retval = (endptr[0] == '\0'); |
| 483 | break; |
| 484 | } |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | if (retval) |
| 489 | attach_option(opt_list, option, opt, length); |
| 490 | } while (retval && option->flags & OPTION_LIST); |
| 491 | |
| 492 | return retval; |
| 493 | } |