common.c 23 KB

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