common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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: add an option to the opt_list */
  349. #if !ENABLE_UDHCPC6
  350. #define attach_option(opt_list, optflag, buffer, length, dhcpv6) \
  351. attach_option(opt_list, optflag, buffer, length)
  352. #endif
  353. static NOINLINE void attach_option(
  354. struct option_set **opt_list,
  355. const struct dhcp_optflag *optflag,
  356. char *buffer,
  357. int length,
  358. bool dhcpv6)
  359. {
  360. IF_NOT_UDHCPC6(bool dhcpv6 = 0;)
  361. struct option_set *existing;
  362. char *allocated = NULL;
  363. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_BIN) {
  364. const char *end;
  365. allocated = xstrdup(buffer); /* more than enough */
  366. end = hex2bin(allocated, buffer, 255);
  367. if (errno)
  368. bb_error_msg_and_die("malformed hex string '%s'", buffer);
  369. length = end - allocated;
  370. }
  371. #if ENABLE_FEATURE_UDHCP_RFC3397
  372. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) {
  373. /* reuse buffer and length for RFC1035-formatted string */
  374. allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
  375. }
  376. #endif
  377. existing = udhcp_find_option(*opt_list, optflag->code);
  378. if (!existing) {
  379. struct option_set *new, **curr;
  380. /* make a new option */
  381. log2("attaching option %02x to list", optflag->code);
  382. new = xmalloc(sizeof(*new));
  383. if (!dhcpv6) {
  384. new->data = xmalloc(length + OPT_DATA);
  385. new->data[OPT_CODE] = optflag->code;
  386. new->data[OPT_LEN] = length;
  387. memcpy(new->data + OPT_DATA, (allocated ? allocated : buffer),
  388. length);
  389. } else {
  390. new->data = xmalloc(length + D6_OPT_DATA);
  391. new->data[D6_OPT_CODE] = optflag->code >> 8;
  392. new->data[D6_OPT_CODE + 1] = optflag->code & 0xff;
  393. new->data[D6_OPT_LEN] = length >> 8;
  394. new->data[D6_OPT_LEN + 1] = length & 0xff;
  395. memcpy(new->data + D6_OPT_DATA, (allocated ? allocated : buffer),
  396. length);
  397. }
  398. curr = opt_list;
  399. while (*curr && (*curr)->data[OPT_CODE] < optflag->code)
  400. curr = &(*curr)->next;
  401. new->next = *curr;
  402. *curr = new;
  403. goto ret;
  404. }
  405. if (optflag->flags & OPTION_LIST) {
  406. unsigned old_len;
  407. /* add it to an existing option */
  408. log2("attaching option %02x to existing member of list", optflag->code);
  409. old_len = existing->data[OPT_LEN];
  410. if (old_len + length < 255) {
  411. /* actually 255 is ok too, but adding a space can overlow it */
  412. existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
  413. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING
  414. || (optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING_HOST
  415. ) {
  416. /* add space separator between STRING options in a list */
  417. existing->data[OPT_DATA + old_len] = ' ';
  418. old_len++;
  419. }
  420. memcpy(existing->data + OPT_DATA + old_len, (allocated ? allocated : buffer), length);
  421. existing->data[OPT_LEN] = old_len + length;
  422. } /* else, ignore the data, we could put this in a second option in the future */
  423. } /* else, ignore the new data */
  424. ret:
  425. free(allocated);
  426. }
  427. int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
  428. const struct dhcp_optflag *optflags, const char *option_strings,
  429. bool dhcpv6)
  430. {
  431. struct option_set **opt_list = arg;
  432. char *opt;
  433. char *str;
  434. const struct dhcp_optflag *optflag;
  435. struct dhcp_optflag userdef_optflag;
  436. unsigned optcode;
  437. int retval;
  438. /* IP_PAIR needs 8 bytes, STATIC_ROUTES needs 9 max */
  439. char buffer[9] ALIGNED(4);
  440. uint16_t *result_u16 = (uint16_t *) buffer;
  441. uint32_t *result_u32 = (uint32_t *) buffer;
  442. /* Cheat, the only *const* str possible is "" */
  443. str = (char *) const_str;
  444. opt = strtok(str, " \t=:");
  445. if (!opt)
  446. return 0;
  447. optcode = bb_strtou(opt, NULL, 0);
  448. if (!errno && optcode < 255) {
  449. /* Raw (numeric) option code.
  450. * Initially assume binary (hex-str), but if "str" or 'str'
  451. * is seen later, switch to STRING.
  452. */
  453. userdef_optflag.flags = OPTION_BIN;
  454. userdef_optflag.code = optcode;
  455. optflag = &userdef_optflag;
  456. } else {
  457. optflag = &optflags[udhcp_option_idx(opt, option_strings)];
  458. }
  459. /* Loop to handle OPTION_LIST case, else execute just once */
  460. retval = 0;
  461. do {
  462. int length;
  463. char *val;
  464. if (optflag->flags == OPTION_BIN) {
  465. val = strtok(NULL, ""); /* do not split "'q w e'" */
  466. trim(val);
  467. } else
  468. val = strtok(NULL, ", \t");
  469. if (!val)
  470. break;
  471. length = dhcp_option_lengths[optflag->flags & OPTION_TYPE_MASK];
  472. retval = 0;
  473. opt = buffer; /* new meaning for variable opt */
  474. switch (optflag->flags & OPTION_TYPE_MASK) {
  475. case OPTION_IP:
  476. retval = udhcp_str2nip(val, buffer);
  477. break;
  478. case OPTION_IP_PAIR:
  479. retval = udhcp_str2nip(val, buffer);
  480. val = strtok(NULL, ", \t/-");
  481. if (!val)
  482. retval = 0;
  483. if (retval)
  484. retval = udhcp_str2nip(val, buffer + 4);
  485. break;
  486. case_OPTION_STRING:
  487. case OPTION_STRING:
  488. case OPTION_STRING_HOST:
  489. #if ENABLE_FEATURE_UDHCP_RFC3397
  490. case OPTION_DNS_STRING:
  491. #endif
  492. length = strnlen(val, 254);
  493. if (length > 0) {
  494. opt = val;
  495. retval = 1;
  496. }
  497. break;
  498. // case OPTION_BOOLEAN: {
  499. // static const char no_yes[] ALIGN1 = "no\0yes\0";
  500. // buffer[0] = retval = index_in_strings(no_yes, val);
  501. // retval++; /* 0 - bad; 1: "no" 2: "yes" */
  502. // break;
  503. // }
  504. case OPTION_U8:
  505. buffer[0] = bb_strtou32(val, NULL, 0);
  506. retval = (errno == 0);
  507. break;
  508. /* htonX are macros in older libc's, using temp var
  509. * in code below for safety */
  510. /* TODO: use bb_strtoX? */
  511. case OPTION_U16: {
  512. uint32_t tmp = bb_strtou32(val, NULL, 0);
  513. *result_u16 = htons(tmp);
  514. retval = (errno == 0 /*&& tmp < 0x10000*/);
  515. break;
  516. }
  517. // case OPTION_S16: {
  518. // long tmp = bb_strtoi32(val, NULL, 0);
  519. // *result_u16 = htons(tmp);
  520. // retval = (errno == 0);
  521. // break;
  522. // }
  523. case OPTION_U32: {
  524. uint32_t tmp = bb_strtou32(val, NULL, 0);
  525. *result_u32 = htonl(tmp);
  526. retval = (errno == 0);
  527. break;
  528. }
  529. case OPTION_S32: {
  530. int32_t tmp = bb_strtoi32(val, NULL, 0);
  531. *result_u32 = htonl(tmp);
  532. retval = (errno == 0);
  533. break;
  534. }
  535. case OPTION_STATIC_ROUTES: {
  536. /* Input: "a.b.c.d/m" */
  537. /* Output: mask(1 byte),pfx(0-4 bytes),gw(4 bytes) */
  538. unsigned mask;
  539. char *slash = strchr(val, '/');
  540. if (slash) {
  541. *slash = '\0';
  542. retval = udhcp_str2nip(val, buffer + 1);
  543. buffer[0] = mask = bb_strtou(slash + 1, NULL, 10);
  544. val = strtok(NULL, ", \t/-");
  545. if (!val || mask > 32 || errno)
  546. retval = 0;
  547. if (retval) {
  548. length = ((mask + 7) >> 3) + 5;
  549. retval = udhcp_str2nip(val, buffer + (length - 4));
  550. }
  551. }
  552. break;
  553. }
  554. case OPTION_BIN:
  555. /* Raw (numeric) option code. Is it a string? */
  556. if (val[0] == '"' || val[0] == '\'') {
  557. char delim = val[0];
  558. char *end = last_char_is(val + 1, delim);
  559. if (end) {
  560. *end = '\0';
  561. val++;
  562. userdef_optflag.flags = OPTION_STRING;
  563. goto case_OPTION_STRING;
  564. }
  565. }
  566. /* No: hex-str option, handled in attach_option() */
  567. opt = val;
  568. retval = 1;
  569. break;
  570. default:
  571. break;
  572. }
  573. if (retval)
  574. attach_option(opt_list, optflag, opt, length, dhcpv6);
  575. } while (retval && (optflag->flags & OPTION_LIST));
  576. return retval;
  577. }
  578. /* note: ip is a pointer to an IPv6 in network order, possibly misaliged */
  579. int FAST_FUNC sprint_nip6(char *dest, /*const char *pre,*/ const uint8_t *ip)
  580. {
  581. char hexstrbuf[16 * 2];
  582. bin2hex(hexstrbuf, (void*)ip, 16);
  583. return sprintf(dest, /* "%s" */
  584. "%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s",
  585. /* pre, */
  586. hexstrbuf + 0 * 4,
  587. hexstrbuf + 1 * 4,
  588. hexstrbuf + 2 * 4,
  589. hexstrbuf + 3 * 4,
  590. hexstrbuf + 4 * 4,
  591. hexstrbuf + 5 * 4,
  592. hexstrbuf + 6 * 4,
  593. hexstrbuf + 7 * 4
  594. );
  595. }