d6_dhcpc.c 55 KB

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