common.c 21 KB

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