3
0

common.c 24 KB

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