common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. { 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. /* Get an option with bounds checking (warning, result is not aligned) */
  212. uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code)
  213. {
  214. uint8_t *optionptr;
  215. int len;
  216. int rem;
  217. int overload = 0;
  218. enum {
  219. FILE_FIELD101 = FILE_FIELD * 0x101,
  220. SNAME_FIELD101 = SNAME_FIELD * 0x101,
  221. };
  222. /* option bytes: [code][len][data1][data2]..[dataLEN] */
  223. optionptr = packet->options;
  224. rem = sizeof(packet->options);
  225. while (1) {
  226. if (rem <= 0) {
  227. complain:
  228. bb_simple_error_msg("bad packet, malformed option field");
  229. return NULL;
  230. }
  231. /* DHCP_PADDING and DHCP_END have no [len] byte */
  232. if (optionptr[OPT_CODE] == DHCP_PADDING) {
  233. rem--;
  234. optionptr++;
  235. continue;
  236. }
  237. if (optionptr[OPT_CODE] == DHCP_END) {
  238. if ((overload & FILE_FIELD101) == FILE_FIELD) {
  239. /* can use packet->file, and didn't look at it yet */
  240. overload |= FILE_FIELD101; /* "we looked at it" */
  241. optionptr = packet->file;
  242. rem = sizeof(packet->file);
  243. continue;
  244. }
  245. if ((overload & SNAME_FIELD101) == SNAME_FIELD) {
  246. /* can use packet->sname, and didn't look at it yet */
  247. overload |= SNAME_FIELD101; /* "we looked at it" */
  248. optionptr = packet->sname;
  249. rem = sizeof(packet->sname);
  250. continue;
  251. }
  252. break;
  253. }
  254. if (rem <= OPT_LEN)
  255. goto complain; /* complain and return NULL */
  256. len = 2 + optionptr[OPT_LEN];
  257. rem -= len;
  258. if (rem < 0)
  259. goto complain; /* complain and return NULL */
  260. if (optionptr[OPT_CODE] == code) {
  261. if (optionptr[OPT_LEN] == 0) {
  262. /* So far no valid option with length 0 known.
  263. * Having this check means that searching
  264. * for DHCP_MESSAGE_TYPE need not worry
  265. * that returned pointer might be unsafe
  266. * to dereference.
  267. */
  268. goto complain; /* complain and return NULL */
  269. }
  270. log_option("option found", optionptr);
  271. return optionptr + OPT_DATA;
  272. }
  273. if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) {
  274. if (len >= 3)
  275. overload |= optionptr[OPT_DATA];
  276. /* fall through */
  277. }
  278. optionptr += len;
  279. }
  280. /* log3 because udhcpc uses it a lot - very noisy */
  281. log3("option 0x%02x not found", code);
  282. return NULL;
  283. }
  284. uint8_t* FAST_FUNC udhcp_get_option32(struct dhcp_packet *packet, int code)
  285. {
  286. uint8_t *r = udhcp_get_option(packet, code);
  287. if (r) {
  288. if (r[-OPT_DATA + OPT_LEN] != 4)
  289. r = NULL;
  290. }
  291. return r;
  292. }
  293. /* Return the position of the 'end' option (no bounds checking) */
  294. int FAST_FUNC udhcp_end_option(uint8_t *optionptr)
  295. {
  296. int i = 0;
  297. while (optionptr[i] != DHCP_END) {
  298. if (optionptr[i] != DHCP_PADDING)
  299. i += optionptr[i + OPT_LEN] + OPT_DATA-1;
  300. i++;
  301. }
  302. return i;
  303. }
  304. /* Add an option (supplied in binary form) to the options.
  305. * Option format: [code][len][data1][data2]..[dataLEN]
  306. */
  307. void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
  308. {
  309. unsigned len;
  310. uint8_t *optionptr = packet->options;
  311. unsigned end = udhcp_end_option(optionptr);
  312. len = OPT_DATA + addopt[OPT_LEN];
  313. /* end position + (option code/length + addopt length) + end option */
  314. if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) {
  315. //TODO: learn how to use overflow option if we exhaust packet->options[]
  316. bb_error_msg("option 0x%02x did not fit into the packet",
  317. addopt[OPT_CODE]);
  318. return;
  319. }
  320. log_option("adding option", addopt);
  321. memcpy(optionptr + end, addopt, len);
  322. optionptr[end + len] = DHCP_END;
  323. }
  324. #if ENABLE_UDHCPC || ENABLE_UDHCPD
  325. /* Add an one to four byte option to a packet */
  326. void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data)
  327. {
  328. const struct dhcp_optflag *dh;
  329. for (dh = dhcp_optflags; dh->code; dh++) {
  330. if (dh->code == code) {
  331. uint8_t option[6], len;
  332. option[OPT_CODE] = code;
  333. len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK];
  334. option[OPT_LEN] = len;
  335. if (BB_BIG_ENDIAN)
  336. data <<= 8 * (4 - len);
  337. /* Assignment is unaligned! */
  338. move_to_unaligned32(&option[OPT_DATA], data);
  339. udhcp_add_binary_option(packet, option);
  340. return;
  341. }
  342. }
  343. bb_error_msg("can't add option 0x%02x", code);
  344. }
  345. #endif
  346. /* Find option 'code' in opt_list */
  347. struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code)
  348. {
  349. while (opt_list && opt_list->data[OPT_CODE] < code)
  350. opt_list = opt_list->next;
  351. if (opt_list && opt_list->data[OPT_CODE] == code)
  352. return opt_list;
  353. return NULL;
  354. }
  355. /* Parse string to IP in network order */
  356. int FAST_FUNC udhcp_str2nip(const char *str, void *arg)
  357. {
  358. len_and_sockaddr *lsa;
  359. lsa = host_and_af2sockaddr(str, 0, AF_INET);
  360. if (!lsa)
  361. return 0;
  362. /* arg maybe unaligned */
  363. move_to_unaligned32((uint32_t*)arg, lsa->u.sin.sin_addr.s_addr);
  364. free(lsa);
  365. return 1;
  366. }
  367. /* udhcp_str2optset:
  368. * Parse string option representation to binary form and add it to opt_list.
  369. * Called to parse "udhcpc -x OPTNAME:OPTVAL"
  370. * and to parse udhcpd.conf's "opt OPTNAME OPTVAL" directives.
  371. */
  372. /* helper: add an option to the opt_list */
  373. #if !ENABLE_UDHCPC6
  374. #define attach_option(opt_list, optflag, buffer, length, dhcpv6) \
  375. attach_option(opt_list, optflag, buffer, length)
  376. #endif
  377. static NOINLINE void attach_option(
  378. struct option_set **opt_list,
  379. const struct dhcp_optflag *optflag,
  380. char *buffer,
  381. int length,
  382. bool dhcpv6)
  383. {
  384. IF_NOT_UDHCPC6(bool dhcpv6 = 0;)
  385. struct option_set *existing;
  386. char *allocated = NULL;
  387. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_BIN) {
  388. const char *end;
  389. allocated = xstrdup(buffer); /* more than enough */
  390. end = hex2bin(allocated, buffer, 255);
  391. if (errno)
  392. bb_error_msg_and_die("malformed hex string '%s'", buffer);
  393. length = end - allocated;
  394. buffer = allocated;
  395. }
  396. #if ENABLE_FEATURE_UDHCP_RFC3397
  397. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) {
  398. /* reuse buffer and length for RFC1035-formatted string */
  399. allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length);
  400. }
  401. #endif
  402. existing = udhcp_find_option(*opt_list, optflag->code);
  403. if (!existing) {
  404. struct option_set *new, **curr;
  405. /* make a new option */
  406. log2("attaching option %02x to list", optflag->code);
  407. new = xmalloc(sizeof(*new));
  408. if (!dhcpv6) {
  409. new->data = xmalloc(length + OPT_DATA);
  410. new->data[OPT_CODE] = optflag->code;
  411. new->data[OPT_LEN] = length;
  412. memcpy(new->data + OPT_DATA, buffer, length);
  413. } else {
  414. new->data = xmalloc(length + D6_OPT_DATA);
  415. new->data[D6_OPT_CODE] = optflag->code >> 8;
  416. new->data[D6_OPT_CODE + 1] = optflag->code & 0xff;
  417. new->data[D6_OPT_LEN] = length >> 8;
  418. new->data[D6_OPT_LEN + 1] = length & 0xff;
  419. memcpy(new->data + D6_OPT_DATA, buffer,
  420. length);
  421. }
  422. curr = opt_list;
  423. while (*curr && (*curr)->data[OPT_CODE] < optflag->code)
  424. curr = &(*curr)->next;
  425. new->next = *curr;
  426. *curr = new;
  427. goto ret;
  428. }
  429. if (optflag->flags & OPTION_LIST) {
  430. unsigned old_len;
  431. /* add it to an existing option */
  432. log2("attaching option %02x to existing member of list", optflag->code);
  433. old_len = existing->data[OPT_LEN];
  434. if (old_len + length < 255) {
  435. /* actually 255 is ok too, but adding a space can overlow it */
  436. existing->data = xrealloc(existing->data, OPT_DATA + 1 + old_len + length);
  437. // So far dhcp_optflags[] has no OPTION_STRING[_HOST] | OPTION_LIST items
  438. #if 0
  439. if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING
  440. || (optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING_HOST
  441. ) {
  442. /* add space separator between STRING options in a list */
  443. existing->data[OPT_DATA + old_len] = ' ';
  444. old_len++;
  445. }
  446. #endif
  447. memcpy(existing->data + OPT_DATA + old_len, buffer, length);
  448. existing->data[OPT_LEN] = old_len + length;
  449. } /* else, ignore the data, we could put this in a second option in the future */
  450. } /* else, ignore the new data */
  451. ret:
  452. free(allocated);
  453. }
  454. int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg,
  455. const struct dhcp_optflag *optflags, const char *option_strings,
  456. bool dhcpv6)
  457. {
  458. struct option_set **opt_list = arg;
  459. char *opt;
  460. char *str;
  461. const struct dhcp_optflag *optflag;
  462. struct dhcp_optflag userdef_optflag;
  463. unsigned optcode;
  464. int retval;
  465. /* IP_PAIR needs 8 bytes, STATIC_ROUTES needs 9 max */
  466. char buffer[9] ALIGNED(4);
  467. uint16_t *result_u16 = (uint16_t *) buffer;
  468. uint32_t *result_u32 = (uint32_t *) buffer;
  469. /* Cheat, the only *const* str possible is "" */
  470. str = (char *) const_str;
  471. opt = strtok(str, " \t=:");
  472. if (!opt)
  473. return 0;
  474. optcode = bb_strtou(opt, NULL, 0);
  475. if (!errno && optcode < 255) {
  476. /* Raw (numeric) option code.
  477. * Initially assume binary (hex-str), but if "str" or 'str'
  478. * is seen later, switch to STRING.
  479. */
  480. userdef_optflag.flags = OPTION_BIN;
  481. userdef_optflag.code = optcode;
  482. optflag = &userdef_optflag;
  483. } else {
  484. optflag = &optflags[udhcp_option_idx(opt, option_strings)];
  485. }
  486. /* Loop to handle OPTION_LIST case, else execute just once */
  487. retval = 0;
  488. do {
  489. int length;
  490. char *val;
  491. if (optflag->flags == OPTION_BIN) {
  492. val = strtok(NULL, ""); /* do not split "'q w e'" */
  493. if (val) trim(val);
  494. } else
  495. val = strtok(NULL, ", \t");
  496. if (!val)
  497. break;
  498. length = dhcp_option_lengths[optflag->flags & OPTION_TYPE_MASK];
  499. retval = 0;
  500. opt = buffer; /* new meaning for variable opt */
  501. switch (optflag->flags & OPTION_TYPE_MASK) {
  502. case OPTION_IP:
  503. retval = udhcp_str2nip(val, buffer);
  504. break;
  505. case OPTION_IP_PAIR:
  506. retval = udhcp_str2nip(val, buffer);
  507. val = strtok(NULL, ", \t/-");
  508. if (!val)
  509. retval = 0;
  510. if (retval)
  511. retval = udhcp_str2nip(val, buffer + 4);
  512. break;
  513. case_OPTION_STRING:
  514. case OPTION_STRING:
  515. case OPTION_STRING_HOST:
  516. #if ENABLE_FEATURE_UDHCP_RFC3397
  517. case OPTION_DNS_STRING:
  518. #endif
  519. length = strnlen(val, 254);
  520. if (length > 0) {
  521. opt = val;
  522. retval = 1;
  523. }
  524. break;
  525. // case OPTION_BOOLEAN: {
  526. // static const char no_yes[] ALIGN1 = "no\0yes\0";
  527. // buffer[0] = retval = index_in_strings(no_yes, val);
  528. // retval++; /* 0 - bad; 1: "no" 2: "yes" */
  529. // break;
  530. // }
  531. case OPTION_U8:
  532. buffer[0] = bb_strtou32(val, NULL, 0);
  533. retval = (errno == 0);
  534. break;
  535. /* htonX are macros in older libc's, using temp var
  536. * in code below for safety */
  537. /* TODO: use bb_strtoX? */
  538. case OPTION_U16: {
  539. uint32_t tmp = bb_strtou32(val, NULL, 0);
  540. *result_u16 = htons(tmp);
  541. retval = (errno == 0 /*&& tmp < 0x10000*/);
  542. break;
  543. }
  544. // case OPTION_S16: {
  545. // long tmp = bb_strtoi32(val, NULL, 0);
  546. // *result_u16 = htons(tmp);
  547. // retval = (errno == 0);
  548. // break;
  549. // }
  550. case OPTION_U32: {
  551. uint32_t tmp = bb_strtou32(val, NULL, 0);
  552. *result_u32 = htonl(tmp);
  553. retval = (errno == 0);
  554. break;
  555. }
  556. case OPTION_S32: {
  557. int32_t tmp = bb_strtoi32(val, NULL, 0);
  558. *result_u32 = htonl(tmp);
  559. retval = (errno == 0);
  560. break;
  561. }
  562. case OPTION_STATIC_ROUTES: {
  563. /* Input: "a.b.c.d/m" */
  564. /* Output: mask(1 byte),pfx(0-4 bytes),gw(4 bytes) */
  565. unsigned mask;
  566. char *slash = strchr(val, '/');
  567. if (slash) {
  568. *slash = '\0';
  569. retval = udhcp_str2nip(val, buffer + 1);
  570. buffer[0] = mask = bb_strtou(slash + 1, NULL, 10);
  571. val = strtok(NULL, ", \t/-");
  572. if (!val || mask > 32 || errno)
  573. retval = 0;
  574. if (retval) {
  575. length = ((mask + 7) >> 3) + 5;
  576. retval = udhcp_str2nip(val, buffer + (length - 4));
  577. }
  578. }
  579. break;
  580. }
  581. case OPTION_BIN:
  582. /* Raw (numeric) option code. Is it a string? */
  583. if (val[0] == '"' || val[0] == '\'') {
  584. char delim = val[0];
  585. char *end = last_char_is(val + 1, delim);
  586. if (end) {
  587. *end = '\0';
  588. val++;
  589. userdef_optflag.flags = OPTION_STRING;
  590. goto case_OPTION_STRING;
  591. }
  592. }
  593. /* No: hex-str option, handled in attach_option() */
  594. opt = val;
  595. retval = 1;
  596. break;
  597. default:
  598. break;
  599. }
  600. if (retval)
  601. attach_option(opt_list, optflag, opt, length, dhcpv6);
  602. } while (retval && (optflag->flags & OPTION_LIST));
  603. return retval;
  604. }
  605. /* note: ip is a pointer to an IPv6 in network order, possibly misaliged */
  606. int FAST_FUNC sprint_nip6(char *dest, /*const char *pre,*/ const uint8_t *ip)
  607. {
  608. char hexstrbuf[16 * 2];
  609. bin2hex(hexstrbuf, (void*)ip, 16);
  610. return sprintf(dest, /* "%s" */
  611. "%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s",
  612. /* pre, */
  613. hexstrbuf + 0 * 4,
  614. hexstrbuf + 1 * 4,
  615. hexstrbuf + 2 * 4,
  616. hexstrbuf + 3 * 4,
  617. hexstrbuf + 4 * 4,
  618. hexstrbuf + 5 * 4,
  619. hexstrbuf + 6 * 4,
  620. hexstrbuf + 7 * 4
  621. );
  622. }