3
0

d6_dhcpc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * DHCPv6 client.
  4. *
  5. * WARNING: THIS CODE IS INCOMPLETE.
  6. *
  7. * Copyright (C) 2011-2017 Denys Vlasenko.
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this source tree.
  10. */
  11. //config:config UDHCPC6
  12. //config: bool "udhcpc6"
  13. //config: default n # not yet ready
  14. //config: depends on FEATURE_IPV6
  15. //config: help
  16. //config: udhcpc6 is a DHCPv6 client
  17. //config:
  18. //config:config FEATURE_UDHCPC6_RFC3646
  19. //config: bool "Support RFC 3646 (DNS server and search list)"
  20. //config: default y
  21. //config: depends on UDHCPC6
  22. //config: help
  23. //config: List of DNS servers and domain search list can be requested with
  24. //config: "-O dns" and "-O search". If server gives these values,
  25. //config: they will be set in environment variables "dns" and "search".
  26. //config:
  27. //config:config FEATURE_UDHCPC6_RFC4704
  28. //config: bool "Support RFC 4704 (Client FQDN)"
  29. //config: default y
  30. //config: depends on UDHCPC6
  31. //config: help
  32. //config: You can request FQDN to be given by server using "-O fqdn".
  33. //config:
  34. //config:config FEATURE_UDHCPC6_RFC4833
  35. //config: bool "Support RFC 4833 (Timezones)"
  36. //config: default y
  37. //config: depends on UDHCPC6
  38. //config: help
  39. //config: You can request POSIX timezone with "-O tz" and timezone name
  40. //config: with "-O timezone".
  41. //config:
  42. //config:config FEATURE_UDHCPC6_RFC5970
  43. //config: bool "Support RFC 5970 (Network Boot)"
  44. //config: default y
  45. //config: depends on UDHCPC6
  46. //config: help
  47. //config: You can request bootfile-url with "-O bootfile_url" and
  48. //config: bootfile-params with "-O bootfile_params".
  49. //applet:IF_UDHCPC6(APPLET(udhcpc6, BB_DIR_USR_BIN, BB_SUID_DROP))
  50. //kbuild:lib-$(CONFIG_UDHCPC6) += d6_dhcpc.o d6_packet.o d6_socket.o common.o socket.o signalpipe.o
  51. //kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC3646) += domain_codec.o
  52. //kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC4704) += domain_codec.o
  53. #include <syslog.h>
  54. /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
  55. #define WANT_PIDFILE 1
  56. #include "common.h"
  57. #include "dhcpd.h"
  58. #include "dhcpc.h"
  59. #include "d6_common.h"
  60. #include <netinet/if_ether.h>
  61. #include <netpacket/packet.h>
  62. #include <linux/filter.h>
  63. /* "struct client_config_t client_config" is in bb_common_bufsiz1 */
  64. static const struct dhcp_optflag d6_optflags[] = {
  65. #if ENABLE_FEATURE_UDHCPC6_RFC3646
  66. { OPTION_6RD | OPTION_LIST | OPTION_REQ, D6_OPT_DNS_SERVERS },
  67. { OPTION_DNS_STRING | OPTION_LIST | OPTION_REQ, D6_OPT_DOMAIN_LIST },
  68. #endif
  69. #if ENABLE_FEATURE_UDHCPC6_RFC4704
  70. { OPTION_DNS_STRING, D6_OPT_CLIENT_FQDN },
  71. #endif
  72. #if ENABLE_FEATURE_UDHCPC6_RFC4833
  73. { OPTION_STRING, D6_OPT_TZ_POSIX },
  74. { OPTION_STRING, D6_OPT_TZ_NAME },
  75. #endif
  76. #if ENABLE_FEATURE_UDHCPC6_RFC5970
  77. { OPTION_STRING, D6_OPT_BOOT_URL },
  78. { OPTION_STRING, D6_OPT_BOOT_PARAM },
  79. #endif
  80. { OPTION_STRING, 0xd1 }, /* DHCP_PXE_CONF_FILE */
  81. { OPTION_STRING, 0xd2 }, /* DHCP_PXE_PATH_PREFIX */
  82. { 0, 0 }
  83. };
  84. /* Must match d6_optflags[] order */
  85. static const char d6_option_strings[] ALIGN1 =
  86. #if ENABLE_FEATURE_UDHCPC6_RFC3646
  87. "dns" "\0" /* D6_OPT_DNS_SERVERS */
  88. "search" "\0" /* D6_OPT_DOMAIN_LIST */
  89. #endif
  90. #if ENABLE_FEATURE_UDHCPC6_RFC4704
  91. "fqdn" "\0" /* D6_OPT_CLIENT_FQDN */
  92. #endif
  93. #if ENABLE_FEATURE_UDHCPC6_RFC4833
  94. "tz" "\0" /* D6_OPT_TZ_POSIX */
  95. "timezone" "\0" /* D6_OPT_TZ_NAME */
  96. #endif
  97. #if ENABLE_FEATURE_UDHCPC6_RFC5970
  98. "bootfile_url" "\0" /* D6_OPT_BOOT_URL */
  99. "bootfile_param" "\0" /* D6_OPT_BOOT_PARAM */
  100. #endif
  101. "pxeconffile" "\0" /* DHCP_PXE_CONF_FILE */
  102. "pxepathprefix" "\0" /* DHCP_PXE_PATH_PREFIX */
  103. "\0";
  104. #if ENABLE_LONG_OPTS
  105. static const char udhcpc6_longopts[] ALIGN1 =
  106. "interface\0" Required_argument "i"
  107. "now\0" No_argument "n"
  108. "pidfile\0" Required_argument "p"
  109. "quit\0" No_argument "q"
  110. "release\0" No_argument "R"
  111. "request\0" Required_argument "r"
  112. "requestprefix\0" No_argument "d"
  113. "script\0" Required_argument "s"
  114. "timeout\0" Required_argument "T"
  115. "retries\0" Required_argument "t"
  116. "tryagain\0" Required_argument "A"
  117. "syslog\0" No_argument "S"
  118. "request-option\0" Required_argument "O"
  119. "no-default-options\0" No_argument "o"
  120. "foreground\0" No_argument "f"
  121. USE_FOR_MMU(
  122. "background\0" No_argument "b"
  123. )
  124. /// IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
  125. IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
  126. ;
  127. #endif
  128. /* Must match getopt32 option string order */
  129. enum {
  130. OPT_i = 1 << 0,
  131. OPT_n = 1 << 1,
  132. OPT_p = 1 << 2,
  133. OPT_q = 1 << 3,
  134. OPT_R = 1 << 4,
  135. OPT_r = 1 << 5,
  136. OPT_s = 1 << 6,
  137. OPT_T = 1 << 7,
  138. OPT_t = 1 << 8,
  139. OPT_S = 1 << 9,
  140. OPT_A = 1 << 10,
  141. OPT_O = 1 << 11,
  142. OPT_o = 1 << 12,
  143. OPT_x = 1 << 13,
  144. OPT_f = 1 << 14,
  145. OPT_d = 1 << 15,
  146. /* The rest has variable bit positions, need to be clever */
  147. OPTBIT_d = 15,
  148. USE_FOR_MMU( OPTBIT_b,)
  149. ///IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
  150. IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
  151. USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
  152. ///IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
  153. IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
  154. };
  155. #if ENABLE_FEATURE_UDHCPC6_RFC4704
  156. static const char opt_fqdn_req[] = {
  157. (D6_OPT_CLIENT_FQDN >> 8), (D6_OPT_CLIENT_FQDN & 0xff),
  158. 0, 2, /* optlen */
  159. 0, /* flags: */
  160. /* S=0: server SHOULD NOT perform AAAA RR updates */
  161. /* O=0: client MUST set this bit to 0 */
  162. /* N=0: server SHOULD perform updates (PTR RR only in our case, since S=0) */
  163. 0 /* empty DNS-encoded name */
  164. };
  165. #endif
  166. /*** Utility functions ***/
  167. static void *d6_find_option(uint8_t *option, uint8_t *option_end, unsigned code)
  168. {
  169. /* "length minus 4" */
  170. int len_m4 = option_end - option - 4;
  171. while (len_m4 >= 0) {
  172. /* Next option's len is too big? */
  173. if (option[3] > len_m4)
  174. return NULL; /* yes. bogus packet! */
  175. /* So far we treat any opts with code >255
  176. * or len >255 as bogus, and stop at once.
  177. * This simplifies big-endian handling.
  178. */
  179. if (option[0] != 0 || option[2] != 0)
  180. return NULL;
  181. /* Option seems to be valid */
  182. /* Does its code match? */
  183. if (option[1] == code)
  184. return option; /* yes! */
  185. len_m4 -= option[3] + 4;
  186. option += option[3] + 4;
  187. }
  188. return NULL;
  189. }
  190. static void *d6_copy_option(uint8_t *option, uint8_t *option_end, unsigned code)
  191. {
  192. uint8_t *opt = d6_find_option(option, option_end, code);
  193. if (!opt)
  194. return opt;
  195. return xmemdup(opt, opt[3] + 4);
  196. }
  197. /*** Script execution code ***/
  198. static char** new_env(void)
  199. {
  200. client6_data.env_ptr = xrealloc_vector(client6_data.env_ptr, 3, client6_data.env_idx);
  201. return &client6_data.env_ptr[client6_data.env_idx++];
  202. }
  203. static char *string_option_to_env(uint8_t *option, uint8_t *option_end)
  204. {
  205. const char *ptr, *name = NULL;
  206. unsigned val_len;
  207. int i;
  208. ptr = d6_option_strings;
  209. i = 0;
  210. while (*ptr) {
  211. if (d6_optflags[i].code == option[1]) {
  212. name = ptr;
  213. goto found;
  214. }
  215. ptr += strlen(ptr) + 1;
  216. i++;
  217. }
  218. bb_error_msg("can't find option name for 0x%x, skipping", option[1]);
  219. return NULL;
  220. found:
  221. val_len = (option[2] << 8) | option[3];
  222. if (val_len + &option[D6_OPT_DATA] > option_end) {
  223. bb_error_msg("option data exceeds option length");
  224. return NULL;
  225. }
  226. return xasprintf("%s=%.*s", name, val_len, (char*)option + 4);
  227. }
  228. /* put all the parameters into the environment */
  229. static void option_to_env(uint8_t *option, uint8_t *option_end)
  230. {
  231. #if ENABLE_FEATURE_UDHCPC6_RFC3646
  232. int addrs, option_offset;
  233. #endif
  234. /* "length minus 4" */
  235. int len_m4 = option_end - option - 4;
  236. while (len_m4 >= 0) {
  237. uint32_t v32;
  238. char ipv6str[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")];
  239. if (option[0] != 0 || option[2] != 0)
  240. break;
  241. /* Check if option-length exceeds size of option */
  242. if (option[3] > len_m4)
  243. break;
  244. switch (option[1]) {
  245. //case D6_OPT_CLIENTID:
  246. //case D6_OPT_SERVERID:
  247. case D6_OPT_IA_NA:
  248. case D6_OPT_IA_PD:
  249. option_to_env(option + 16, option + 4 + option[3]);
  250. break;
  251. //case D6_OPT_IA_TA:
  252. case D6_OPT_IAADDR:
  253. /* 0 1 2 3
  254. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  255. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  256. * | OPTION_IAADDR | option-len |
  257. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  258. * | |
  259. * | IPv6 address |
  260. * | |
  261. * | |
  262. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  263. * | preferred-lifetime |
  264. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  265. * | valid-lifetime |
  266. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  267. */
  268. sprint_nip6(ipv6str, option + 4);
  269. *new_env() = xasprintf("ipv6=%s", ipv6str);
  270. move_from_unaligned32(v32, option + 4 + 16 + 4);
  271. *new_env() = xasprintf("lease=%u", (unsigned)v32);
  272. break;
  273. //case D6_OPT_ORO:
  274. //case D6_OPT_PREFERENCE:
  275. //case D6_OPT_ELAPSED_TIME:
  276. //case D6_OPT_RELAY_MSG:
  277. //case D6_OPT_AUTH:
  278. //case D6_OPT_UNICAST:
  279. //case D6_OPT_STATUS_CODE:
  280. //case D6_OPT_RAPID_COMMIT:
  281. //case D6_OPT_USER_CLASS:
  282. //case D6_OPT_VENDOR_CLASS:
  283. //case D6_OPT_VENDOR_OPTS:
  284. //case D6_OPT_INTERFACE_ID:
  285. //case D6_OPT_RECONF_MSG:
  286. //case D6_OPT_RECONF_ACCEPT:
  287. case D6_OPT_IAPREFIX:
  288. /* 0 1 2 3
  289. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  290. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  291. * | OPTION_IAPREFIX | option-length |
  292. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  293. * | preferred-lifetime |
  294. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  295. * | valid-lifetime |
  296. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  297. * | prefix-length | |
  298. * +-+-+-+-+-+-+-+-+ IPv6 prefix |
  299. * | (16 octets) |
  300. * | |
  301. * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  302. * | |
  303. * +-+-+-+-+-+-+-+-+
  304. */
  305. move_from_unaligned32(v32, option + 4 + 4);
  306. *new_env() = xasprintf("ipv6prefix_lease=%u", (unsigned)v32);
  307. sprint_nip6(ipv6str, option + 4 + 4 + 4 + 1);
  308. *new_env() = xasprintf("ipv6prefix=%s/%u", ipv6str, (unsigned)(option[4 + 4 + 4]));
  309. break;
  310. #if ENABLE_FEATURE_UDHCPC6_RFC3646
  311. case D6_OPT_DNS_SERVERS: {
  312. char *dlist;
  313. /* Make sure payload-size is a multiple of 16 */
  314. if ((option[3] & 0x0f) != 0)
  315. break;
  316. /* Get the number of addresses on the option */
  317. addrs = option[3] >> 4;
  318. /* Setup environment variable */
  319. *new_env() = dlist = xmalloc(4 + addrs * 40 - 1);
  320. dlist = stpcpy(dlist, "dns=");
  321. option_offset = 0;
  322. while (addrs--) {
  323. sprint_nip6(dlist, option + 4 + option_offset);
  324. dlist += 39;
  325. option_offset += 16;
  326. if (addrs)
  327. *dlist++ = ' ';
  328. }
  329. break;
  330. }
  331. case D6_OPT_DOMAIN_LIST: {
  332. char *dlist;
  333. dlist = dname_dec(option + 4, (option[2] << 8) | option[3], "search=");
  334. if (!dlist)
  335. break;
  336. *new_env() = dlist;
  337. break;
  338. }
  339. #endif
  340. #if ENABLE_FEATURE_UDHCPC6_RFC4704
  341. case D6_OPT_CLIENT_FQDN: {
  342. char *dlist;
  343. if (option[3] == 0)
  344. break;
  345. /* Work around broken ISC DHCPD6.
  346. * ISC DHCPD6 does not implement RFC 4704 correctly: It says the first
  347. * byte of option-payload should contain flags where the bits 7-3 are
  348. * reserved for future use and MUST be zero. Instead ISC DHCPD6 just
  349. * writes the entire FQDN as string to option-payload. We assume a
  350. * broken server here if any of the reserved bits are set.
  351. */
  352. if (option[4] & 0xf8) {
  353. *new_env() = xasprintf("fqdn=%.*s", (int)option[3], (char*)option + 4);
  354. break;
  355. }
  356. dlist = dname_dec(option + 5, (/*(option[2] << 8) |*/ option[3]) - 1, "fqdn=");
  357. if (!dlist)
  358. break;
  359. *new_env() = dlist;
  360. break;
  361. }
  362. #endif
  363. #if ENABLE_FEATURE_UDHCPC6_RFC4833
  364. /* RFC 4833 Timezones */
  365. case D6_OPT_TZ_POSIX:
  366. *new_env() = xasprintf("tz=%.*s", (int)option[3], (char*)option + 4);
  367. break;
  368. case D6_OPT_TZ_NAME:
  369. *new_env() = xasprintf("tz_name=%.*s", (int)option[3], (char*)option + 4);
  370. break;
  371. #endif
  372. case D6_OPT_BOOT_URL:
  373. case D6_OPT_BOOT_PARAM:
  374. case 0xd1: /* DHCP_PXE_CONF_FILE */
  375. case 0xd2: /* DHCP_PXE_PATH_PREFIX */
  376. {
  377. char *tmp = string_option_to_env(option, option_end);
  378. if (tmp)
  379. *new_env() = tmp;
  380. break;
  381. }
  382. }
  383. len_m4 -= 4 + option[3];
  384. option += 4 + option[3];
  385. }
  386. }
  387. static char **fill_envp(struct d6_packet *packet)
  388. {
  389. char **envp, **curr;
  390. client6_data.env_ptr = NULL;
  391. client6_data.env_idx = 0;
  392. *new_env() = xasprintf("interface=%s", client_config.interface);
  393. if (packet)
  394. option_to_env(packet->d6_options, packet->d6_options + sizeof(packet->d6_options));
  395. envp = curr = client6_data.env_ptr;
  396. while (*curr)
  397. putenv(*curr++);
  398. return envp;
  399. }
  400. /* Call a script with a par file and env vars */
  401. static void d6_run_script(struct d6_packet *packet, const char *name)
  402. {
  403. char **envp, **curr;
  404. char *argv[3];
  405. envp = fill_envp(packet);
  406. /* call script */
  407. log1("executing %s %s", client_config.script, name);
  408. argv[0] = (char*) client_config.script;
  409. argv[1] = (char*) name;
  410. argv[2] = NULL;
  411. spawn_and_wait(argv);
  412. for (curr = envp; *curr; curr++) {
  413. log2(" %s", *curr);
  414. bb_unsetenv_and_free(*curr);
  415. }
  416. free(envp);
  417. }
  418. /*** Sending/receiving packets ***/
  419. static ALWAYS_INLINE uint32_t random_xid(void)
  420. {
  421. uint32_t t = rand() & htonl(0x00ffffff);
  422. return t;
  423. }
  424. /* Initialize the packet with the proper defaults */
  425. static uint8_t *init_d6_packet(struct d6_packet *packet, char type, uint32_t xid)
  426. {
  427. struct d6_option *clientid;
  428. memset(packet, 0, sizeof(*packet));
  429. packet->d6_xid32 = xid;
  430. packet->d6_msg_type = type;
  431. clientid = (void*)client_config.clientid;
  432. return mempcpy(packet->d6_options, clientid, clientid->len + 2+2);
  433. }
  434. static uint8_t *add_d6_client_options(uint8_t *ptr)
  435. {
  436. struct option_set *curr;
  437. uint8_t *start = ptr;
  438. unsigned option;
  439. uint16_t len;
  440. ptr += 4;
  441. for (option = 1; option < 256; option++) {
  442. if (client_config.opt_mask[option >> 3] & (1 << (option & 7))) {
  443. ptr[0] = (option >> 8);
  444. ptr[1] = option;
  445. ptr += 2;
  446. }
  447. }
  448. if ((ptr - start - 4) != 0) {
  449. start[0] = (D6_OPT_ORO >> 8);
  450. start[1] = D6_OPT_ORO;
  451. start[2] = ((ptr - start - 4) >> 8);
  452. start[3] = (ptr - start - 4);
  453. } else
  454. ptr = start;
  455. #if ENABLE_FEATURE_UDHCPC6_RFC4704
  456. ptr = mempcpy(ptr, &opt_fqdn_req, sizeof(opt_fqdn_req));
  457. #endif
  458. /* Add -x options if any */
  459. curr = client_config.options;
  460. while (curr) {
  461. len = (curr->data[D6_OPT_LEN] << 8) | curr->data[D6_OPT_LEN + 1];
  462. ptr = mempcpy(ptr, curr->data, D6_OPT_DATA + len);
  463. curr = curr->next;
  464. }
  465. return ptr;
  466. }
  467. static int d6_mcast_from_client_config_ifindex(struct d6_packet *packet, uint8_t *end)
  468. {
  469. static const uint8_t FF02__1_2[16] = {
  470. 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,
  472. };
  473. return d6_send_raw_packet(
  474. packet, (end - (uint8_t*) packet),
  475. /*src*/ &client6_data.ll_ip6, CLIENT_PORT6,
  476. /*dst*/ (struct in6_addr*)FF02__1_2, SERVER_PORT6, MAC_BCAST_ADDR,
  477. client_config.ifindex
  478. );
  479. }
  480. /* Milticast a DHCPv6 Solicit packet to the network, with an optionally requested IP.
  481. *
  482. * RFC 3315 17.1.1. Creation of Solicit Messages
  483. *
  484. * The client MUST include a Client Identifier option to identify itself
  485. * to the server. The client includes IA options for any IAs to which
  486. * it wants the server to assign addresses. The client MAY include
  487. * addresses in the IAs as a hint to the server about addresses for
  488. * which the client has a preference. ...
  489. *
  490. * The client uses IA_NA options to request the assignment of non-
  491. * temporary addresses and uses IA_TA options to request the assignment
  492. * of temporary addresses. Either IA_NA or IA_TA options, or a
  493. * combination of both, can be included in DHCP messages.
  494. *
  495. * The client SHOULD include an Option Request option (see section 22.7)
  496. * to indicate the options the client is interested in receiving. The
  497. * client MAY additionally include instances of those options that are
  498. * identified in the Option Request option, with data values as hints to
  499. * the server about parameter values the client would like to have
  500. * returned.
  501. *
  502. * The client includes a Reconfigure Accept option (see section 22.20)
  503. * if the client is willing to accept Reconfigure messages from the
  504. * server.
  505. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  506. | OPTION_CLIENTID | option-len |
  507. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  508. . .
  509. . DUID .
  510. . (variable length) .
  511. . .
  512. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  513. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  514. | OPTION_IA_NA | option-len |
  515. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  516. | IAID (4 octets) |
  517. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  518. | T1 |
  519. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  520. | T2 |
  521. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  522. | |
  523. . IA_NA-options .
  524. . .
  525. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  526. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  527. | OPTION_IAADDR | option-len |
  528. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  529. | |
  530. | IPv6 address |
  531. | |
  532. | |
  533. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  534. | preferred-lifetime |
  535. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  536. | valid-lifetime |
  537. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  538. . .
  539. . IAaddr-options .
  540. . .
  541. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  542. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  543. | OPTION_ORO | option-len |
  544. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  545. | requested-option-code-1 | requested-option-code-2 |
  546. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  547. | ... |
  548. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  549. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  550. | OPTION_RECONF_ACCEPT | 0 |
  551. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  552. */
  553. /* NOINLINE: limit stack usage in caller */
  554. static NOINLINE int send_d6_discover(uint32_t xid, struct in6_addr *requested_ipv6)
  555. {
  556. struct d6_packet packet;
  557. uint8_t *opt_ptr;
  558. unsigned len;
  559. /* Fill in: msg type, client id */
  560. opt_ptr = init_d6_packet(&packet, D6_MSG_SOLICIT, xid);
  561. /* Create new IA_NA, optionally with included IAADDR with requested IP */
  562. free(client6_data.ia_na);
  563. client6_data.ia_na = NULL;
  564. if (option_mask32 & OPT_r) {
  565. len = requested_ipv6 ? 2+2+4+4+4 + 2+2+16+4+4 : 2+2+4+4+4;
  566. client6_data.ia_na = xzalloc(len);
  567. client6_data.ia_na->code = D6_OPT_IA_NA;
  568. client6_data.ia_na->len = len - 4;
  569. *(uint32_t*)client6_data.ia_na->data = rand(); /* IAID */
  570. if (requested_ipv6) {
  571. struct d6_option *iaaddr = (void*)(client6_data.ia_na->data + 4+4+4);
  572. iaaddr->code = D6_OPT_IAADDR;
  573. iaaddr->len = 16+4+4;
  574. memcpy(iaaddr->data, requested_ipv6, 16);
  575. }
  576. opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, len);
  577. }
  578. /* IA_PD */
  579. free(client6_data.ia_pd);
  580. client6_data.ia_pd = NULL;
  581. if (option_mask32 & OPT_d) {
  582. len = 2+2+4+4+4;
  583. client6_data.ia_pd = xzalloc(len);
  584. client6_data.ia_pd->code = D6_OPT_IA_PD;
  585. client6_data.ia_pd->len = len - 4;
  586. *(uint32_t*)client6_data.ia_pd->data = rand(); /* IAID */
  587. opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, len);
  588. }
  589. /* Add options:
  590. * "param req" option according to -O, options specified with -x
  591. */
  592. opt_ptr = add_d6_client_options(opt_ptr);
  593. bb_error_msg("sending %s", "discover");
  594. return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
  595. }
  596. /* Multicast a DHCPv6 request message
  597. *
  598. * RFC 3315 18.1.1. Creation and Transmission of Request Messages
  599. *
  600. * The client uses a Request message to populate IAs with addresses and
  601. * obtain other configuration information. The client includes one or
  602. * more IA options in the Request message. The server then returns
  603. * addresses and other information about the IAs to the client in IA
  604. * options in a Reply message.
  605. *
  606. * The client generates a transaction ID and inserts this value in the
  607. * "transaction-id" field.
  608. *
  609. * The client places the identifier of the destination server in a
  610. * Server Identifier option.
  611. *
  612. * The client MUST include a Client Identifier option to identify itself
  613. * to the server. The client adds any other appropriate options,
  614. * including one or more IA options (if the client is requesting that
  615. * the server assign it some network addresses).
  616. *
  617. * The client MUST include an Option Request option (see section 22.7)
  618. * to indicate the options the client is interested in receiving. The
  619. * client MAY include options with data values as hints to the server
  620. * about parameter values the client would like to have returned.
  621. *
  622. * The client includes a Reconfigure Accept option (see section 22.20)
  623. * indicating whether or not the client is willing to accept Reconfigure
  624. * messages from the server.
  625. */
  626. /* NOINLINE: limit stack usage in caller */
  627. static NOINLINE int send_d6_select(uint32_t xid)
  628. {
  629. struct d6_packet packet;
  630. uint8_t *opt_ptr;
  631. /* Fill in: msg type, client id */
  632. opt_ptr = init_d6_packet(&packet, D6_MSG_REQUEST, xid);
  633. /* server id */
  634. opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
  635. /* IA NA (contains requested IP) */
  636. if (client6_data.ia_na)
  637. opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
  638. /* IA PD */
  639. if (client6_data.ia_pd)
  640. opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
  641. /* Add options:
  642. * "param req" option according to -O, options specified with -x
  643. */
  644. opt_ptr = add_d6_client_options(opt_ptr);
  645. bb_error_msg("sending %s", "select");
  646. return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
  647. }
  648. /* Unicast or broadcast a DHCP renew message
  649. *
  650. * RFC 3315 18.1.3. Creation and Transmission of Renew Messages
  651. *
  652. * To extend the valid and preferred lifetimes for the addresses
  653. * associated with an IA, the client sends a Renew message to the server
  654. * from which the client obtained the addresses in the IA containing an
  655. * IA option for the IA. The client includes IA Address options in the
  656. * IA option for the addresses associated with the IA. The server
  657. * determines new lifetimes for the addresses in the IA according to the
  658. * administrative configuration of the server. The server may also add
  659. * new addresses to the IA. The server may remove addresses from the IA
  660. * by setting the preferred and valid lifetimes of those addresses to
  661. * zero.
  662. *
  663. * The server controls the time at which the client contacts the server
  664. * to extend the lifetimes on assigned addresses through the T1 and T2
  665. * parameters assigned to an IA.
  666. *
  667. * At time T1 for an IA, the client initiates a Renew/Reply message
  668. * exchange to extend the lifetimes on any addresses in the IA. The
  669. * client includes an IA option with all addresses currently assigned to
  670. * the IA in its Renew message.
  671. *
  672. * If T1 or T2 is set to 0 by the server (for an IA_NA) or there are no
  673. * T1 or T2 times (for an IA_TA), the client may send a Renew or Rebind
  674. * message, respectively, at the client's discretion.
  675. *
  676. * The client sets the "msg-type" field to RENEW. The client generates
  677. * a transaction ID and inserts this value in the "transaction-id"
  678. * field.
  679. *
  680. * The client places the identifier of the destination server in a
  681. * Server Identifier option.
  682. *
  683. * The client MUST include a Client Identifier option to identify itself
  684. * to the server. The client adds any appropriate options, including
  685. * one or more IA options. The client MUST include the list of
  686. * addresses the client currently has associated with the IAs in the
  687. * Renew message.
  688. *
  689. * The client MUST include an Option Request option (see section 22.7)
  690. * to indicate the options the client is interested in receiving. The
  691. * client MAY include options with data values as hints to the server
  692. * about parameter values the client would like to have returned.
  693. */
  694. /* NOINLINE: limit stack usage in caller */
  695. static NOINLINE int send_d6_renew(uint32_t xid, struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
  696. {
  697. struct d6_packet packet;
  698. uint8_t *opt_ptr;
  699. /* Fill in: msg type, client id */
  700. opt_ptr = init_d6_packet(&packet, DHCPREQUEST, xid);
  701. /* server id */
  702. opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
  703. /* IA NA (contains requested IP) */
  704. if (client6_data.ia_na)
  705. opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
  706. /* IA PD */
  707. if (client6_data.ia_pd)
  708. opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
  709. /* Add options:
  710. * "param req" option according to -O, options specified with -x
  711. */
  712. opt_ptr = add_d6_client_options(opt_ptr);
  713. bb_error_msg("sending %s", "renew");
  714. if (server_ipv6) {
  715. return d6_send_kernel_packet(
  716. &packet, (opt_ptr - (uint8_t*) &packet),
  717. our_cur_ipv6, CLIENT_PORT6,
  718. server_ipv6, SERVER_PORT6,
  719. client_config.ifindex
  720. /* TODO? send_flags: MSG_DONTROUTE (see IPv4 code for reason why) */
  721. );
  722. }
  723. return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
  724. }
  725. /* Unicast a DHCP release message */
  726. static int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
  727. {
  728. struct d6_packet packet;
  729. uint8_t *opt_ptr;
  730. /* Fill in: msg type, client id */
  731. opt_ptr = init_d6_packet(&packet, D6_MSG_RELEASE, random_xid());
  732. /* server id */
  733. opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
  734. /* IA NA (contains our current IP) */
  735. if (client6_data.ia_na)
  736. opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
  737. /* IA PD */
  738. if (client6_data.ia_pd)
  739. opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
  740. bb_error_msg("sending %s", "release");
  741. return d6_send_kernel_packet(
  742. &packet, (opt_ptr - (uint8_t*) &packet),
  743. our_cur_ipv6, CLIENT_PORT6,
  744. server_ipv6, SERVER_PORT6,
  745. client_config.ifindex
  746. );
  747. }
  748. /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
  749. /* NOINLINE: limit stack usage in caller */
  750. static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6, struct d6_packet *d6_pkt, int fd)
  751. {
  752. int bytes;
  753. struct ip6_udp_d6_packet packet;
  754. bytes = safe_read(fd, &packet, sizeof(packet));
  755. if (bytes < 0) {
  756. log1("packet read error, ignoring");
  757. /* NB: possible down interface, etc. Caller should pause. */
  758. return bytes; /* returns -1 */
  759. }
  760. if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) {
  761. log1("packet is too short, ignoring");
  762. return -2;
  763. }
  764. if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) {
  765. /* packet is bigger than sizeof(packet), we did partial read */
  766. log1("oversized packet, ignoring");
  767. return -2;
  768. }
  769. /* ignore any extra garbage bytes */
  770. bytes = sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen);
  771. /* make sure its the right packet for us, and that it passes sanity checks */
  772. if (packet.ip6.ip6_nxt != IPPROTO_UDP
  773. || (packet.ip6.ip6_vfc >> 4) != 6
  774. || packet.udp.dest != htons(CLIENT_PORT6)
  775. /* || bytes > (int) sizeof(packet) - can't happen */
  776. || packet.udp.len != packet.ip6.ip6_plen
  777. ) {
  778. log1("unrelated/bogus packet, ignoring");
  779. return -2;
  780. }
  781. //How to do this for ipv6?
  782. // /* verify UDP checksum. IP header has to be modified for this */
  783. // memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
  784. // /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
  785. // packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
  786. // check = packet.udp.check;
  787. // packet.udp.check = 0;
  788. // if (check && check != inet_cksum((uint16_t *)&packet, bytes)) {
  789. // log1("packet with bad UDP checksum received, ignoring");
  790. // return -2;
  791. // }
  792. if (peer_ipv6)
  793. *peer_ipv6 = packet.ip6.ip6_src; /* struct copy */
  794. log1("received %s", "a packet");
  795. d6_dump_packet(&packet.data);
  796. bytes -= sizeof(packet.ip6) + sizeof(packet.udp);
  797. memcpy(d6_pkt, &packet.data, bytes);
  798. return bytes;
  799. }
  800. /*** Main ***/
  801. static int sockfd = -1;
  802. #define LISTEN_NONE 0
  803. #define LISTEN_KERNEL 1
  804. #define LISTEN_RAW 2
  805. static smallint listen_mode;
  806. /* initial state: (re)start DHCP negotiation */
  807. #define INIT_SELECTING 0
  808. /* discover was sent, DHCPOFFER reply received */
  809. #define REQUESTING 1
  810. /* select/renew was sent, DHCPACK reply received */
  811. #define BOUND 2
  812. /* half of lease passed, want to renew it by sending unicast renew requests */
  813. #define RENEWING 3
  814. /* renew requests were not answered, lease is almost over, send broadcast renew */
  815. #define REBINDING 4
  816. /* manually requested renew (SIGUSR1) */
  817. #define RENEW_REQUESTED 5
  818. /* release, possibly manually requested (SIGUSR2) */
  819. #define RELEASED 6
  820. static smallint state;
  821. static int d6_raw_socket(int ifindex)
  822. {
  823. int fd;
  824. struct sockaddr_ll sock;
  825. /*
  826. * Comment:
  827. *
  828. * I've selected not to see LL header, so BPF doesn't see it, too.
  829. * The filter may also pass non-IP and non-ARP packets, but we do
  830. * a more complete check when receiving the message in userspace.
  831. *
  832. * and filter shamelessly stolen from:
  833. *
  834. * http://www.flamewarmaster.de/software/dhcpclient/
  835. *
  836. * There are a few other interesting ideas on that page (look under
  837. * "Motivation"). Use of netlink events is most interesting. Think
  838. * of various network servers listening for events and reconfiguring.
  839. * That would obsolete sending HUP signals and/or make use of restarts.
  840. *
  841. * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
  842. * License: GPL v2.
  843. *
  844. * TODO: make conditional?
  845. */
  846. #if 0
  847. static const struct sock_filter filter_instr[] = {
  848. /* load 9th byte (protocol) */
  849. BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
  850. /* jump to L1 if it is IPPROTO_UDP, else to L4 */
  851. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
  852. /* L1: load halfword from offset 6 (flags and frag offset) */
  853. BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
  854. /* jump to L4 if any bits in frag offset field are set, else to L2 */
  855. BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
  856. /* L2: skip IP header (load index reg with header len) */
  857. BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0),
  858. /* load udp destination port from halfword[header_len + 2] */
  859. BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
  860. /* jump to L3 if udp dport is CLIENT_PORT6, else to L4 */
  861. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
  862. /* L3: accept packet */
  863. BPF_STMT(BPF_RET|BPF_K, 0x7fffffff),
  864. /* L4: discard packet */
  865. BPF_STMT(BPF_RET|BPF_K, 0),
  866. };
  867. static const struct sock_fprog filter_prog = {
  868. .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
  869. /* casting const away: */
  870. .filter = (struct sock_filter *) filter_instr,
  871. };
  872. #endif
  873. log2("opening raw socket on ifindex %d", ifindex);
  874. fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  875. log2("got raw socket fd %d", fd);
  876. memset(&sock, 0, sizeof(sock)); /* let's be deterministic */
  877. sock.sll_family = AF_PACKET;
  878. sock.sll_protocol = htons(ETH_P_IPV6);
  879. sock.sll_ifindex = ifindex;
  880. /*sock.sll_hatype = ARPHRD_???;*/
  881. /*sock.sll_pkttype = PACKET_???;*/
  882. /*sock.sll_halen = ???;*/
  883. /*sock.sll_addr[8] = ???;*/
  884. xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
  885. #if 0
  886. if (CLIENT_PORT6 == 546) {
  887. /* Use only if standard port is in use */
  888. /* Ignoring error (kernel may lack support for this) */
  889. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
  890. sizeof(filter_prog)) >= 0)
  891. log1("attached filter to raw socket fd %d", fd); // log?
  892. }
  893. #endif
  894. log1("created raw socket");
  895. return fd;
  896. }
  897. static void change_listen_mode(int new_mode)
  898. {
  899. log1("entering listen mode: %s",
  900. new_mode != LISTEN_NONE
  901. ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw")
  902. : "none"
  903. );
  904. listen_mode = new_mode;
  905. if (sockfd >= 0) {
  906. close(sockfd);
  907. sockfd = -1;
  908. }
  909. if (new_mode == LISTEN_KERNEL)
  910. sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface);
  911. else if (new_mode != LISTEN_NONE)
  912. sockfd = d6_raw_socket(client_config.ifindex);
  913. /* else LISTEN_NONE: sockfd stays closed */
  914. }
  915. /* Called only on SIGUSR1 */
  916. static void perform_renew(void)
  917. {
  918. bb_error_msg("performing DHCP renew");
  919. switch (state) {
  920. case BOUND:
  921. change_listen_mode(LISTEN_KERNEL);
  922. case RENEWING:
  923. case REBINDING:
  924. state = RENEW_REQUESTED;
  925. break;
  926. case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
  927. d6_run_script(NULL, "deconfig");
  928. case REQUESTING:
  929. case RELEASED:
  930. change_listen_mode(LISTEN_RAW);
  931. state = INIT_SELECTING;
  932. break;
  933. case INIT_SELECTING:
  934. break;
  935. }
  936. }
  937. static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
  938. {
  939. /* send release packet */
  940. if (state == BOUND
  941. || state == RENEWING
  942. || state == REBINDING
  943. || state == RENEW_REQUESTED
  944. ) {
  945. bb_error_msg("unicasting a release");
  946. send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */
  947. }
  948. bb_error_msg("entering released state");
  949. /*
  950. * We can be here on: SIGUSR2,
  951. * or on exit (SIGTERM) and -R "release on quit" is specified.
  952. * Users requested to be notified in all cases, even if not in one
  953. * of the states above.
  954. */
  955. d6_run_script(NULL, "deconfig");
  956. change_listen_mode(LISTEN_NONE);
  957. state = RELEASED;
  958. }
  959. ///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
  960. ///{
  961. /// uint8_t *storage;
  962. /// int len = strnlen(str, 255);
  963. /// storage = xzalloc(len + extra + OPT_DATA);
  964. /// storage[OPT_CODE] = code;
  965. /// storage[OPT_LEN] = len + extra;
  966. /// memcpy(storage + extra + OPT_DATA, str, len);
  967. /// return storage;
  968. ///}
  969. #if BB_MMU
  970. static void client_background(void)
  971. {
  972. bb_daemonize(0);
  973. logmode &= ~LOGMODE_STDIO;
  974. /* rewrite pidfile, as our pid is different now */
  975. write_pidfile(client_config.pidfile);
  976. }
  977. #endif
  978. //usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  979. //usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__
  980. //usage:#else
  981. //usage:# define IF_UDHCP_VERBOSE(...)
  982. //usage:#endif
  983. //usage:#define udhcpc6_trivial_usage
  984. //usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"odR] [-i IFACE] [-r IPv6] [-s PROG] [-p PIDFILE]\n"
  985. //usage: " [-x OPT:VAL]... [-O OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]")
  986. //usage:#define udhcpc6_full_usage "\n"
  987. //usage: "\n -i IFACE Interface to use (default eth0)"
  988. //usage: "\n -p FILE Create pidfile"
  989. //usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
  990. //usage: "\n -B Request broadcast replies"
  991. //usage: "\n -t N Send up to N discover packets"
  992. //usage: "\n -T N Pause between packets (default 3 seconds)"
  993. //usage: "\n -A N Wait N seconds (default 20) after failure"
  994. //usage: "\n -f Run in foreground"
  995. //usage: USE_FOR_MMU(
  996. //usage: "\n -b Background if lease is not obtained"
  997. //usage: )
  998. //usage: "\n -n Exit if lease is not obtained"
  999. //usage: "\n -q Exit after obtaining lease"
  1000. //usage: "\n -R Release IP on exit"
  1001. //usage: "\n -S Log to syslog too"
  1002. //usage: IF_FEATURE_UDHCP_PORT(
  1003. //usage: "\n -P N Use port N (default 546)"
  1004. //usage: )
  1005. ////usage: IF_FEATURE_UDHCPC_ARPING(
  1006. ////usage: "\n -a Use arping to validate offered address"
  1007. ////usage: )
  1008. //usage: "\n -O OPT Request option OPT from server (cumulative)"
  1009. //usage: "\n -o Don't request any options (unless -O is given)"
  1010. //usage: "\n -r IPv6 Request this address ('no' to not request any IP)"
  1011. //usage: "\n -d Request prefix"
  1012. //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
  1013. //usage: "\n Examples of string, numeric, and hex byte opts:"
  1014. //usage: "\n -x hostname:bbox - option 12"
  1015. //usage: "\n -x lease:3600 - option 51 (lease time)"
  1016. //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
  1017. //usage: "\n -x 14:'\"dumpfile\"' - option 14 (shell-quoted)"
  1018. //usage: IF_UDHCP_VERBOSE(
  1019. //usage: "\n -v Verbose"
  1020. //usage: )
  1021. //usage: "\nSignals:"
  1022. //usage: "\n USR1 Renew lease"
  1023. //usage: "\n USR2 Release lease"
  1024. int udhcpc6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1025. int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
  1026. {
  1027. const char *str_r;
  1028. IF_FEATURE_UDHCP_PORT(char *str_P;)
  1029. void *clientid_mac_ptr;
  1030. llist_t *list_O = NULL;
  1031. llist_t *list_x = NULL;
  1032. int tryagain_timeout = 20;
  1033. int discover_timeout = 3;
  1034. int discover_retries = 3;
  1035. struct in6_addr srv6_buf;
  1036. struct in6_addr ipv6_buf;
  1037. struct in6_addr *requested_ipv6;
  1038. uint32_t xid = 0;
  1039. int packet_num;
  1040. int timeout; /* must be signed */
  1041. unsigned already_waited_sec;
  1042. unsigned opt;
  1043. int retval;
  1044. setup_common_bufsiz();
  1045. /* We want random_xid to be random */
  1046. srand(monotonic_us());
  1047. /* Default options */
  1048. IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;)
  1049. IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;)
  1050. client_config.interface = "eth0";
  1051. client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
  1052. /* Parse command line */
  1053. opt = getopt32long(argv, "^"
  1054. /* O,x: list; -T,-t,-A take numeric param */
  1055. "i:np:qRr:s:T:+t:+SA:+O:*ox:*fd"
  1056. USE_FOR_MMU("b")
  1057. ///IF_FEATURE_UDHCPC_ARPING("a")
  1058. IF_FEATURE_UDHCP_PORT("P:")
  1059. "v"
  1060. "\0" IF_UDHCP_VERBOSE("vv") /* -v is a counter */
  1061. , udhcpc6_longopts
  1062. , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
  1063. , &client_config.script /* s */
  1064. , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
  1065. , &list_O
  1066. , &list_x
  1067. IF_FEATURE_UDHCP_PORT(, &str_P)
  1068. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  1069. );
  1070. requested_ipv6 = NULL;
  1071. option_mask32 |= OPT_r;
  1072. if (opt & OPT_r) {
  1073. if (strcmp(str_r, "no") == 0) {
  1074. option_mask32 -= OPT_r;
  1075. } else {
  1076. if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0)
  1077. bb_error_msg_and_die("bad IPv6 address '%s'", str_r);
  1078. requested_ipv6 = &ipv6_buf;
  1079. }
  1080. }
  1081. #if ENABLE_FEATURE_UDHCP_PORT
  1082. if (opt & OPT_P) {
  1083. CLIENT_PORT6 = xatou16(str_P);
  1084. SERVER_PORT6 = CLIENT_PORT6 + 1;
  1085. }
  1086. #endif
  1087. while (list_O) {
  1088. char *optstr = llist_pop(&list_O);
  1089. unsigned n = bb_strtou(optstr, NULL, 0);
  1090. if (errno || n > 254) {
  1091. n = udhcp_option_idx(optstr, d6_option_strings);
  1092. n = d6_optflags[n].code;
  1093. }
  1094. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1095. }
  1096. if (!(opt & OPT_o)) {
  1097. unsigned i, n;
  1098. for (i = 0; (n = d6_optflags[i].code) != 0; i++) {
  1099. if (d6_optflags[i].flags & OPTION_REQ) {
  1100. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1101. }
  1102. }
  1103. }
  1104. while (list_x) {
  1105. char *optstr = xstrdup(llist_pop(&list_x));
  1106. udhcp_str2optset(optstr, &client_config.options,
  1107. d6_optflags, d6_option_strings,
  1108. /*dhcpv6:*/ 1
  1109. );
  1110. free(optstr);
  1111. }
  1112. if (d6_read_interface(client_config.interface,
  1113. &client_config.ifindex,
  1114. &client6_data.ll_ip6,
  1115. client_config.client_mac)
  1116. ) {
  1117. return 1;
  1118. }
  1119. /* Create client ID based on mac, set clientid_mac_ptr */
  1120. {
  1121. struct d6_option *clientid;
  1122. clientid = xzalloc(2+2+2+2+6);
  1123. clientid->code = D6_OPT_CLIENTID;
  1124. clientid->len = 2+2+6;
  1125. clientid->data[1] = 3; /* DUID-LL */
  1126. clientid->data[3] = 1; /* ethernet */
  1127. clientid_mac_ptr = clientid->data + 2+2;
  1128. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1129. client_config.clientid = (void*)clientid;
  1130. }
  1131. #if !BB_MMU
  1132. /* on NOMMU reexec (i.e., background) early */
  1133. if (!(opt & OPT_f)) {
  1134. bb_daemonize_or_rexec(0 /* flags */, argv);
  1135. logmode = LOGMODE_NONE;
  1136. }
  1137. #endif
  1138. if (opt & OPT_S) {
  1139. openlog(applet_name, LOG_PID, LOG_DAEMON);
  1140. logmode |= LOGMODE_SYSLOG;
  1141. }
  1142. /* Make sure fd 0,1,2 are open */
  1143. bb_sanitize_stdio();
  1144. /* Create pidfile */
  1145. write_pidfile(client_config.pidfile);
  1146. /* Goes to stdout (unless NOMMU) and possibly syslog */
  1147. bb_error_msg("started, v"BB_VER);
  1148. /* Set up the signal pipe */
  1149. udhcp_sp_setup();
  1150. state = INIT_SELECTING;
  1151. d6_run_script(NULL, "deconfig");
  1152. change_listen_mode(LISTEN_RAW);
  1153. packet_num = 0;
  1154. timeout = 0;
  1155. already_waited_sec = 0;
  1156. /* Main event loop. select() waits on signal pipe and possibly
  1157. * on sockfd.
  1158. * "continue" statements in code below jump to the top of the loop.
  1159. */
  1160. for (;;) {
  1161. int tv;
  1162. struct pollfd pfds[2];
  1163. struct d6_packet packet;
  1164. uint8_t *packet_end;
  1165. /* silence "uninitialized!" warning */
  1166. unsigned timestamp_before_wait = timestamp_before_wait;
  1167. //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
  1168. /* Was opening raw or udp socket here
  1169. * if (listen_mode != LISTEN_NONE && sockfd < 0),
  1170. * but on fast network renew responses return faster
  1171. * than we open sockets. Thus this code is moved
  1172. * to change_listen_mode(). Thus we open listen socket
  1173. * BEFORE we send renew request (see "case BOUND:"). */
  1174. udhcp_sp_fd_set(pfds, sockfd);
  1175. tv = timeout - already_waited_sec;
  1176. retval = 0;
  1177. /* If we already timed out, fall through with retval = 0, else... */
  1178. if (tv > 0) {
  1179. log1("waiting %u seconds", tv);
  1180. timestamp_before_wait = (unsigned)monotonic_sec();
  1181. retval = poll(pfds, 2, tv < INT_MAX/1000 ? tv * 1000 : INT_MAX);
  1182. if (retval < 0) {
  1183. /* EINTR? A signal was caught, don't panic */
  1184. if (errno == EINTR) {
  1185. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1186. continue;
  1187. }
  1188. /* Else: an error occured, panic! */
  1189. bb_perror_msg_and_die("poll");
  1190. }
  1191. }
  1192. /* If timeout dropped to zero, time to become active:
  1193. * resend discover/renew/whatever
  1194. */
  1195. if (retval == 0) {
  1196. /* When running on a bridge, the ifindex may have changed
  1197. * (e.g. if member interfaces were added/removed
  1198. * or if the status of the bridge changed).
  1199. * Refresh ifindex and client_mac:
  1200. */
  1201. if (d6_read_interface(client_config.interface,
  1202. &client_config.ifindex,
  1203. &client6_data.ll_ip6,
  1204. client_config.client_mac)
  1205. ) {
  1206. goto ret0; /* iface is gone? */
  1207. }
  1208. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1209. /* We will restart the wait in any case */
  1210. already_waited_sec = 0;
  1211. switch (state) {
  1212. case INIT_SELECTING:
  1213. if (!discover_retries || packet_num < discover_retries) {
  1214. if (packet_num == 0)
  1215. xid = random_xid();
  1216. /* multicast */
  1217. send_d6_discover(xid, requested_ipv6);
  1218. timeout = discover_timeout;
  1219. packet_num++;
  1220. continue;
  1221. }
  1222. leasefail:
  1223. d6_run_script(NULL, "leasefail");
  1224. #if BB_MMU /* -b is not supported on NOMMU */
  1225. if (opt & OPT_b) { /* background if no lease */
  1226. bb_error_msg("no lease, forking to background");
  1227. client_background();
  1228. /* do not background again! */
  1229. opt = ((opt & ~OPT_b) | OPT_f);
  1230. } else
  1231. #endif
  1232. if (opt & OPT_n) { /* abort if no lease */
  1233. bb_error_msg("no lease, failing");
  1234. retval = 1;
  1235. goto ret;
  1236. }
  1237. /* wait before trying again */
  1238. timeout = tryagain_timeout;
  1239. packet_num = 0;
  1240. continue;
  1241. case REQUESTING:
  1242. if (!discover_retries || packet_num < discover_retries) {
  1243. /* send multicast select packet */
  1244. send_d6_select(xid);
  1245. timeout = discover_timeout;
  1246. packet_num++;
  1247. continue;
  1248. }
  1249. /* Timed out, go back to init state.
  1250. * "discover...select...discover..." loops
  1251. * were seen in the wild. Treat them similarly
  1252. * to "no response to discover" case */
  1253. change_listen_mode(LISTEN_RAW);
  1254. state = INIT_SELECTING;
  1255. goto leasefail;
  1256. case BOUND:
  1257. /* 1/2 lease passed, enter renewing state */
  1258. state = RENEWING;
  1259. client_config.first_secs = 0; /* make secs field count from 0 */
  1260. change_listen_mode(LISTEN_KERNEL);
  1261. log1("entering renew state");
  1262. /* fall right through */
  1263. case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
  1264. case_RENEW_REQUESTED:
  1265. case RENEWING:
  1266. if (timeout > 60) {
  1267. /* send an unicast renew request */
  1268. /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
  1269. * a new UDP socket for sending inside send_renew.
  1270. * I hazard to guess existing listening socket
  1271. * is somehow conflicting with it, but why is it
  1272. * not deterministic then?! Strange.
  1273. * Anyway, it does recover by eventually failing through
  1274. * into INIT_SELECTING state.
  1275. */
  1276. send_d6_renew(xid, &srv6_buf, requested_ipv6);
  1277. timeout >>= 1;
  1278. continue;
  1279. }
  1280. /* Timed out, enter rebinding state */
  1281. log1("entering rebinding state");
  1282. state = REBINDING;
  1283. /* fall right through */
  1284. case REBINDING:
  1285. /* Switch to bcast receive */
  1286. change_listen_mode(LISTEN_RAW);
  1287. /* Lease is *really* about to run out,
  1288. * try to find DHCP server using broadcast */
  1289. if (timeout > 0) {
  1290. /* send a broadcast renew request */
  1291. send_d6_renew(xid, /*server_ipv6:*/ NULL, requested_ipv6);
  1292. timeout >>= 1;
  1293. continue;
  1294. }
  1295. /* Timed out, enter init state */
  1296. bb_error_msg("lease lost, entering init state");
  1297. d6_run_script(NULL, "deconfig");
  1298. state = INIT_SELECTING;
  1299. client_config.first_secs = 0; /* make secs field count from 0 */
  1300. /*timeout = 0; - already is */
  1301. packet_num = 0;
  1302. continue;
  1303. /* case RELEASED: */
  1304. }
  1305. /* yah, I know, *you* say it would never happen */
  1306. timeout = INT_MAX;
  1307. continue; /* back to main loop */
  1308. } /* if poll timed out */
  1309. /* poll() didn't timeout, something happened */
  1310. /* Is it a signal? */
  1311. switch (udhcp_sp_read()) {
  1312. case SIGUSR1:
  1313. client_config.first_secs = 0; /* make secs field count from 0 */
  1314. already_waited_sec = 0;
  1315. perform_renew();
  1316. if (state == RENEW_REQUESTED) {
  1317. /* We might be either on the same network
  1318. * (in which case renew might work),
  1319. * or we might be on a completely different one
  1320. * (in which case renew won't ever succeed).
  1321. * For the second case, must make sure timeout
  1322. * is not too big, or else we can send
  1323. * futile renew requests for hours.
  1324. * (Ab)use -A TIMEOUT value (usually 20 sec)
  1325. * as a cap on the timeout.
  1326. */
  1327. if (timeout > tryagain_timeout)
  1328. timeout = tryagain_timeout;
  1329. goto case_RENEW_REQUESTED;
  1330. }
  1331. /* Start things over */
  1332. packet_num = 0;
  1333. /* Kill any timeouts, user wants this to hurry along */
  1334. timeout = 0;
  1335. continue;
  1336. case SIGUSR2:
  1337. perform_d6_release(&srv6_buf, requested_ipv6);
  1338. timeout = INT_MAX;
  1339. continue;
  1340. case SIGTERM:
  1341. bb_error_msg("received %s", "SIGTERM");
  1342. goto ret0;
  1343. }
  1344. /* Is it a packet? */
  1345. if (!pfds[1].revents)
  1346. continue; /* no */
  1347. {
  1348. int len;
  1349. /* A packet is ready, read it */
  1350. if (listen_mode == LISTEN_KERNEL)
  1351. len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd);
  1352. else
  1353. len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd);
  1354. if (len == -1) {
  1355. /* Error is severe, reopen socket */
  1356. bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
  1357. sleep(discover_timeout); /* 3 seconds by default */
  1358. change_listen_mode(listen_mode); /* just close and reopen */
  1359. }
  1360. /* If this packet will turn out to be unrelated/bogus,
  1361. * we will go back and wait for next one.
  1362. * Be sure timeout is properly decreased. */
  1363. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1364. if (len < 0)
  1365. continue;
  1366. packet_end = (uint8_t*)&packet + len;
  1367. }
  1368. if ((packet.d6_xid32 & htonl(0x00ffffff)) != xid) {
  1369. log1("xid %x (our is %x), ignoring packet",
  1370. (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)xid);
  1371. continue;
  1372. }
  1373. switch (state) {
  1374. case INIT_SELECTING:
  1375. if (packet.d6_msg_type == D6_MSG_ADVERTISE)
  1376. goto type_is_ok;
  1377. /* DHCPv6 has "Rapid Commit", when instead of Advertise,
  1378. * server sends Reply right away.
  1379. * Fall through to check for this case.
  1380. */
  1381. case REQUESTING:
  1382. case RENEWING:
  1383. case RENEW_REQUESTED:
  1384. case REBINDING:
  1385. if (packet.d6_msg_type == D6_MSG_REPLY) {
  1386. uint32_t lease_seconds;
  1387. struct d6_option *option;
  1388. unsigned address_timeout;
  1389. unsigned prefix_timeout;
  1390. type_is_ok:
  1391. address_timeout = 0;
  1392. prefix_timeout = 0;
  1393. option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE);
  1394. if (option && (option->data[0] | option->data[1]) != 0) {
  1395. /* return to init state */
  1396. bb_error_msg("received DHCP NAK (%u)", option->data[4]);
  1397. d6_run_script(&packet, "nak");
  1398. if (state != REQUESTING)
  1399. d6_run_script(NULL, "deconfig");
  1400. change_listen_mode(LISTEN_RAW);
  1401. sleep(3); /* avoid excessive network traffic */
  1402. state = INIT_SELECTING;
  1403. client_config.first_secs = 0; /* make secs field count from 0 */
  1404. requested_ipv6 = NULL;
  1405. timeout = 0;
  1406. packet_num = 0;
  1407. already_waited_sec = 0;
  1408. continue;
  1409. }
  1410. option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID);
  1411. if (!option) {
  1412. bb_error_msg("no server ID, ignoring packet");
  1413. continue;
  1414. /* still selecting - this server looks bad */
  1415. }
  1416. //Note: we do not bother comparing server IDs in Advertise and Reply msgs.
  1417. //server_id variable is used solely for creation of proper server_id option
  1418. //in outgoing packets. (why DHCPv6 even introduced it is a mystery).
  1419. free(client6_data.server_id);
  1420. client6_data.server_id = option;
  1421. if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
  1422. /* enter requesting state */
  1423. state = REQUESTING;
  1424. timeout = 0;
  1425. packet_num = 0;
  1426. already_waited_sec = 0;
  1427. continue;
  1428. }
  1429. /* It's a D6_MSG_REPLY */
  1430. /*
  1431. * RFC 3315 18.1.8. Receipt of Reply Messages
  1432. *
  1433. * Upon the receipt of a valid Reply message in response to a Solicit
  1434. * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or
  1435. * Information-request message, the client extracts the configuration
  1436. * information contained in the Reply. The client MAY choose to report
  1437. * any status code or message from the status code option in the Reply
  1438. * message.
  1439. *
  1440. * The client SHOULD perform duplicate address detection [17] on each of
  1441. * the addresses in any IAs it receives in the Reply message before
  1442. * using that address for traffic. If any of the addresses are found to
  1443. * be in use on the link, the client sends a Decline message to the
  1444. * server as described in section 18.1.7.
  1445. *
  1446. * If the Reply was received in response to a Solicit (with a Rapid
  1447. * Commit option), Request, Renew or Rebind message, the client updates
  1448. * the information it has recorded about IAs from the IA options
  1449. * contained in the Reply message:
  1450. *
  1451. * - Record T1 and T2 times.
  1452. *
  1453. * - Add any new addresses in the IA option to the IA as recorded by
  1454. * the client.
  1455. *
  1456. * - Update lifetimes for any addresses in the IA option that the
  1457. * client already has recorded in the IA.
  1458. *
  1459. * - Discard any addresses from the IA, as recorded by the client, that
  1460. * have a valid lifetime of 0 in the IA Address option.
  1461. *
  1462. * - Leave unchanged any information about addresses the client has
  1463. * recorded in the IA but that were not included in the IA from the
  1464. * server.
  1465. *
  1466. * Management of the specific configuration information is detailed in
  1467. * the definition of each option in section 22.
  1468. *
  1469. * If the client receives a Reply message with a Status Code containing
  1470. * UnspecFail, the server is indicating that it was unable to process
  1471. * the message due to an unspecified failure condition. If the client
  1472. * retransmits the original message to the same server to retry the
  1473. * desired operation, the client MUST limit the rate at which it
  1474. * retransmits the message and limit the duration of the time during
  1475. * which it retransmits the message.
  1476. *
  1477. * When the client receives a Reply message with a Status Code option
  1478. * with the value UseMulticast, the client records the receipt of the
  1479. * message and sends subsequent messages to the server through the
  1480. * interface on which the message was received using multicast. The
  1481. * client resends the original message using multicast.
  1482. *
  1483. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1484. * | OPTION_IA_NA | option-len |
  1485. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1486. * | IAID (4 octets) |
  1487. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1488. * | T1 |
  1489. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1490. * | T2 |
  1491. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1492. * | |
  1493. * . IA_NA-options .
  1494. * . .
  1495. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1496. *
  1497. *
  1498. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1499. * | OPTION_IAADDR | option-len |
  1500. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1501. * | |
  1502. * | IPv6 address |
  1503. * | |
  1504. * | |
  1505. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1506. * | preferred-lifetime |
  1507. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1508. * | valid-lifetime |
  1509. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1510. * . .
  1511. * . IAaddr-options .
  1512. * . .
  1513. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1514. */
  1515. if (option_mask32 & OPT_r) {
  1516. struct d6_option *iaaddr;
  1517. free(client6_data.ia_na);
  1518. client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA);
  1519. if (!client6_data.ia_na) {
  1520. bb_error_msg("no %s option, ignoring packet", "IA_NA");
  1521. continue;
  1522. }
  1523. if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) {
  1524. bb_error_msg("%s option is too short:%d bytes",
  1525. "IA_NA", client6_data.ia_na->len);
  1526. continue;
  1527. }
  1528. iaaddr = d6_find_option(client6_data.ia_na->data + 4 + 4 + 4,
  1529. client6_data.ia_na->data + client6_data.ia_na->len,
  1530. D6_OPT_IAADDR
  1531. );
  1532. if (!iaaddr) {
  1533. bb_error_msg("no %s option, ignoring packet", "IAADDR");
  1534. continue;
  1535. }
  1536. if (iaaddr->len < (16 + 4 + 4)) {
  1537. bb_error_msg("%s option is too short:%d bytes",
  1538. "IAADDR", iaaddr->len);
  1539. continue;
  1540. }
  1541. /* Note: the address is sufficiently aligned for cast:
  1542. * we _copied_ IA-NA, and copy is always well-aligned.
  1543. */
  1544. requested_ipv6 = (struct in6_addr*) iaaddr->data;
  1545. move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4);
  1546. lease_seconds = ntohl(lease_seconds);
  1547. /// TODO: check for 0 lease time?
  1548. bb_error_msg("%s obtained, lease time %u",
  1549. "IPv6", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
  1550. address_timeout = lease_seconds;
  1551. }
  1552. if (option_mask32 & OPT_d) {
  1553. struct d6_option *iaprefix;
  1554. free(client6_data.ia_pd);
  1555. client6_data.ia_pd = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_PD);
  1556. if (!client6_data.ia_pd) {
  1557. bb_error_msg("no %s option, ignoring packet", "IA_PD");
  1558. continue;
  1559. }
  1560. if (client6_data.ia_pd->len < (4 + 4 + 4) + (2 + 2 + 4 + 4 + 1 + 16)) {
  1561. bb_error_msg("%s option is too short:%d bytes",
  1562. "IA_PD", client6_data.ia_pd->len);
  1563. continue;
  1564. }
  1565. iaprefix = d6_find_option(client6_data.ia_pd->data + 4 + 4 + 4,
  1566. client6_data.ia_pd->data + client6_data.ia_pd->len,
  1567. D6_OPT_IAPREFIX
  1568. );
  1569. if (!iaprefix) {
  1570. bb_error_msg("no %s option, ignoring packet", "IAPREFIX");
  1571. continue;
  1572. }
  1573. if (iaprefix->len < (4 + 4 + 1 + 16)) {
  1574. bb_error_msg("%s option is too short:%d bytes",
  1575. "IAPREFIX", iaprefix->len);
  1576. continue;
  1577. }
  1578. move_from_unaligned32(lease_seconds, iaprefix->data + 4);
  1579. lease_seconds = ntohl(lease_seconds);
  1580. bb_error_msg("%s obtained, lease time %u",
  1581. "prefix", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
  1582. prefix_timeout = lease_seconds;
  1583. }
  1584. if (!address_timeout)
  1585. address_timeout = prefix_timeout;
  1586. if (!prefix_timeout)
  1587. prefix_timeout = address_timeout;
  1588. /* note: "int timeout" will not overflow even with 0xffffffff inputs here: */
  1589. timeout = (prefix_timeout < address_timeout ? prefix_timeout : address_timeout) / 2;
  1590. /* paranoia: must not be too small */
  1591. if (timeout < 0x10)
  1592. timeout = 0x10;
  1593. /* enter bound state */
  1594. d6_run_script(&packet, state == REQUESTING ? "bound" : "renew");
  1595. state = BOUND;
  1596. change_listen_mode(LISTEN_NONE);
  1597. if (opt & OPT_q) { /* quit after lease */
  1598. goto ret0;
  1599. }
  1600. /* future renew failures should not exit (JM) */
  1601. opt &= ~OPT_n;
  1602. #if BB_MMU /* NOMMU case backgrounded earlier */
  1603. if (!(opt & OPT_f)) {
  1604. client_background();
  1605. /* do not background again! */
  1606. opt = ((opt & ~OPT_b) | OPT_f);
  1607. }
  1608. #endif
  1609. already_waited_sec = 0;
  1610. continue; /* back to main loop */
  1611. }
  1612. continue;
  1613. /* case BOUND: - ignore all packets */
  1614. /* case RELEASED: - ignore all packets */
  1615. }
  1616. /* back to main loop */
  1617. } /* for (;;) - main loop ends */
  1618. ret0:
  1619. if (opt & OPT_R) /* release on quit */
  1620. perform_d6_release(&srv6_buf, requested_ipv6);
  1621. retval = 0;
  1622. ret:
  1623. /*if (client_config.pidfile) - remove_pidfile has its own check */
  1624. remove_pidfile(client_config.pidfile);
  1625. return retval;
  1626. }