common.c 20 KB

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