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 | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
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 | */ |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 21 | const struct dhcp_optflag dhcp_optflags[] = { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 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 | */ |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 79 | /* Must match dhcp_optflags[] order */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 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: |
Denys Vlasenko | 5ab6b0f | 2010-04-06 16:52:59 +0200 | [diff] [blame] | 112 | "sipsrv" "\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 | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 179 | /* Get an option with bounds checking (warning, result is not aligned) */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 243 | /* Return the position of the 'end' option (no bounds checking) */ |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 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 | { |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 281 | const struct dhcp_optflag *dh; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 282 | |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 283 | for (dh = dhcp_optflags; dh->code; dh++) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 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: |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 327 | * Parse string option representation to binary form and add it to opt_list. |
| 328 | * Called to parse "udhcpc -x OPTNAME:OPTVAL" |
| 329 | * and to parse udhcpd.conf's "opt OPTNAME OPTVAL" directives. |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 330 | */ |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 331 | /* helper for the helper */ |
| 332 | static char *allocate_tempopt_if_needed( |
| 333 | const struct dhcp_optflag *optflag, |
| 334 | char *buffer, |
| 335 | int *length_p) |
| 336 | { |
| 337 | char *allocated = NULL; |
| 338 | if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_BIN) { |
| 339 | const char *end; |
| 340 | allocated = xstrdup(buffer); /* more than enough */ |
| 341 | end = hex2bin(allocated, buffer, 255); |
| 342 | if (errno) |
| 343 | bb_error_msg_and_die("malformed hex string '%s'", buffer); |
| 344 | *length_p = end - allocated; |
| 345 | } |
| 346 | return allocated; |
| 347 | } |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 348 | /* helper: add an option to the opt_list */ |
| 349 | static NOINLINE void attach_option( |
| 350 | struct option_set **opt_list, |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 351 | const struct dhcp_optflag *optflag, |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 352 | char *buffer, |
| 353 | int length) |
| 354 | { |
| 355 | struct option_set *existing, *new, **curr; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 356 | char *allocated = NULL; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 357 | |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 358 | existing = udhcp_find_option(*opt_list, optflag->code); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 359 | if (!existing) { |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 360 | log2("Attaching option %02x to list", optflag->code); |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 361 | allocated = allocate_tempopt_if_needed(optflag, buffer, &length); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 362 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 363 | if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 364 | /* reuse buffer and length for RFC1035-formatted string */ |
| 365 | allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length); |
| 366 | } |
| 367 | #endif |
| 368 | /* make a new option */ |
| 369 | new = xmalloc(sizeof(*new)); |
| 370 | new->data = xmalloc(length + OPT_DATA); |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 371 | new->data[OPT_CODE] = optflag->code; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 372 | new->data[OPT_LEN] = length; |
| 373 | memcpy(new->data + OPT_DATA, buffer, length); |
| 374 | |
| 375 | curr = opt_list; |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 376 | while (*curr && (*curr)->data[OPT_CODE] < optflag->code) |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 377 | curr = &(*curr)->next; |
| 378 | |
| 379 | new->next = *curr; |
| 380 | *curr = new; |
| 381 | goto ret; |
| 382 | } |
| 383 | |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 384 | if (optflag->flags & OPTION_LIST) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 385 | unsigned old_len; |
| 386 | |
| 387 | /* add it to an existing option */ |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 388 | log2("Attaching option %02x to existing member of list", optflag->code); |
| 389 | allocated = allocate_tempopt_if_needed(optflag, buffer, &length); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 390 | old_len = existing->data[OPT_LEN]; |
| 391 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 392 | if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 393 | /* reuse buffer and length for RFC1035-formatted string */ |
| 394 | allocated = buffer = (char *)dname_enc(existing->data + OPT_DATA, old_len, buffer, &length); |
| 395 | } |
| 396 | #endif |
| 397 | if (old_len + length < 255) { |
| 398 | /* actually 255 is ok too, but adding a space can overlow it */ |
| 399 | |
| 400 | existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length); |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 401 | if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 402 | /* add space separator between STRING options in a list */ |
| 403 | existing->data[OPT_DATA + old_len] = ' '; |
| 404 | old_len++; |
| 405 | } |
| 406 | memcpy(existing->data + OPT_DATA + old_len, buffer, length); |
| 407 | existing->data[OPT_LEN] = old_len + length; |
| 408 | } /* else, ignore the data, we could put this in a second option in the future */ |
| 409 | } /* else, ignore the new data */ |
| 410 | |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 411 | ret: |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 412 | free(allocated); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg) |
| 416 | { |
| 417 | struct option_set **opt_list = arg; |
| 418 | char *opt, *val, *endptr; |
| 419 | char *str; |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 420 | const struct dhcp_optflag *optflag; |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 421 | struct dhcp_optflag bin_optflag; |
| 422 | unsigned optcode; |
Denys Vlasenko | 9107b63 | 2010-03-27 23:23:41 +0100 | [diff] [blame] | 423 | int retval, length; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 424 | char buffer[8] ALIGNED(4); |
| 425 | uint16_t *result_u16 = (uint16_t *) buffer; |
| 426 | uint32_t *result_u32 = (uint32_t *) buffer; |
| 427 | |
| 428 | /* Cheat, the only *const* str possible is "" */ |
| 429 | str = (char *) const_str; |
| 430 | opt = strtok(str, " \t="); |
| 431 | if (!opt) |
| 432 | return 0; |
| 433 | |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 434 | optcode = bb_strtou(opt, NULL, 0); |
| 435 | if (!errno && optcode < 255) { |
| 436 | /* Raw (numeric) option code */ |
| 437 | bin_optflag.flags = OPTION_BIN; |
| 438 | bin_optflag.code = optcode; |
| 439 | optflag = &bin_optflag; |
| 440 | } else { |
| 441 | optflag = &dhcp_optflags[udhcp_option_idx(opt)]; |
| 442 | } |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 443 | |
| 444 | retval = 0; |
| 445 | do { |
| 446 | val = strtok(NULL, ", \t"); |
| 447 | if (!val) |
| 448 | break; |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 449 | length = dhcp_option_lengths[optflag->flags & OPTION_TYPE_MASK]; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 450 | retval = 0; |
| 451 | opt = buffer; /* new meaning for variable opt */ |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 452 | switch (optflag->flags & OPTION_TYPE_MASK) { |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 453 | case OPTION_IP: |
| 454 | retval = udhcp_str2nip(val, buffer); |
| 455 | break; |
| 456 | case OPTION_IP_PAIR: |
| 457 | retval = udhcp_str2nip(val, buffer); |
| 458 | val = strtok(NULL, ", \t/-"); |
| 459 | if (!val) |
| 460 | retval = 0; |
| 461 | if (retval) |
| 462 | retval = udhcp_str2nip(val, buffer + 4); |
| 463 | break; |
| 464 | case OPTION_STRING: |
| 465 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denys Vlasenko | 243ddcb | 2010-04-03 17:34:52 +0200 | [diff] [blame] | 466 | case OPTION_DNS_STRING: |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 467 | #endif |
| 468 | length = strnlen(val, 254); |
| 469 | if (length > 0) { |
| 470 | opt = val; |
| 471 | retval = 1; |
| 472 | } |
| 473 | break; |
| 474 | // case OPTION_BOOLEAN: { |
Denys Vlasenko | 8a659f6 | 2010-04-03 00:52:16 +0200 | [diff] [blame] | 475 | // static const char no_yes[] ALIGN1 = "no\0yes\0"; |
| 476 | // buffer[0] = retval = index_in_strings(no_yes, val); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 477 | // retval++; /* 0 - bad; 1: "no" 2: "yes" */ |
| 478 | // break; |
| 479 | // } |
| 480 | case OPTION_U8: |
| 481 | buffer[0] = strtoul(val, &endptr, 0); |
| 482 | retval = (endptr[0] == '\0'); |
| 483 | break; |
| 484 | /* htonX are macros in older libc's, using temp var |
| 485 | * in code below for safety */ |
| 486 | /* TODO: use bb_strtoX? */ |
| 487 | case OPTION_U16: { |
| 488 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 489 | *result_u16 = htons(tmp); |
| 490 | retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/); |
| 491 | break; |
| 492 | } |
| 493 | // case OPTION_S16: { |
| 494 | // long tmp = strtol(val, &endptr, 0); |
| 495 | // *result_u16 = htons(tmp); |
| 496 | // retval = (endptr[0] == '\0'); |
| 497 | // break; |
| 498 | // } |
| 499 | case OPTION_U32: { |
| 500 | unsigned long tmp = strtoul(val, &endptr, 0); |
| 501 | *result_u32 = htonl(tmp); |
| 502 | retval = (endptr[0] == '\0'); |
| 503 | break; |
| 504 | } |
| 505 | case OPTION_S32: { |
| 506 | long tmp = strtol(val, &endptr, 0); |
| 507 | *result_u32 = htonl(tmp); |
| 508 | retval = (endptr[0] == '\0'); |
| 509 | break; |
| 510 | } |
Denys Vlasenko | 4f3aa51 | 2010-04-04 15:31:12 +0200 | [diff] [blame] | 511 | case OPTION_BIN: /* handled in attach_option() */ |
| 512 | opt = val; |
| 513 | retval = 1; |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 514 | default: |
| 515 | break; |
| 516 | } |
| 517 | if (retval) |
Denys Vlasenko | c03602b | 2010-04-04 15:28:49 +0200 | [diff] [blame] | 518 | attach_option(opt_list, optflag, opt, length); |
| 519 | } while (retval && optflag->flags & OPTION_LIST); |
Denys Vlasenko | a8f6b99 | 2010-03-26 08:35:24 +0100 | [diff] [blame] | 520 | |
| 521 | return retval; |
| 522 | } |