common.c 19 KB

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