d6_dhcpc.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. //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. log2("opening raw socket on ifindex %d", ifindex);
  782. fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  783. log2("got raw socket fd %d", fd);
  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. opt = getopt32long(argv, "^"
  989. /* O,x: list; -T,-t,-A take numeric param */
  990. "i:np:qRr:s:T:+t:+SA:+O:*ox:*f"
  991. USE_FOR_MMU("b")
  992. ///IF_FEATURE_UDHCPC_ARPING("a")
  993. IF_FEATURE_UDHCP_PORT("P:")
  994. "v"
  995. "\0" IF_UDHCP_VERBOSE("vv") /* -v is a counter */
  996. , udhcpc6_longopts
  997. , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
  998. , &client_config.script /* s */
  999. , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
  1000. , &list_O
  1001. , &list_x
  1002. IF_FEATURE_UDHCP_PORT(, &str_P)
  1003. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  1004. );
  1005. requested_ipv6 = NULL;
  1006. if (opt & OPT_r) {
  1007. if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0)
  1008. bb_error_msg_and_die("bad IPv6 address '%s'", str_r);
  1009. requested_ipv6 = &ipv6_buf;
  1010. }
  1011. #if ENABLE_FEATURE_UDHCP_PORT
  1012. if (opt & OPT_P) {
  1013. CLIENT_PORT6 = xatou16(str_P);
  1014. SERVER_PORT6 = CLIENT_PORT6 + 1;
  1015. }
  1016. #endif
  1017. while (list_O) {
  1018. char *optstr = llist_pop(&list_O);
  1019. unsigned n = bb_strtou(optstr, NULL, 0);
  1020. if (errno || n > 254) {
  1021. n = udhcp_option_idx(optstr, d6_option_strings);
  1022. n = d6_optflags[n].code;
  1023. }
  1024. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1025. }
  1026. if (!(opt & OPT_o)) {
  1027. unsigned i, n;
  1028. for (i = 0; (n = d6_optflags[i].code) != 0; i++) {
  1029. if (d6_optflags[i].flags & OPTION_REQ) {
  1030. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1031. }
  1032. }
  1033. }
  1034. while (list_x) {
  1035. char *optstr = llist_pop(&list_x);
  1036. char *colon = strchr(optstr, ':');
  1037. if (colon)
  1038. *colon = ' ';
  1039. /* now it looks similar to udhcpd's config file line:
  1040. * "optname optval", using the common routine: */
  1041. udhcp_str2optset(optstr, &client_config.options, d6_optflags, d6_option_strings);
  1042. if (colon)
  1043. *colon = ':'; /* restore it for NOMMU reexec */
  1044. }
  1045. if (d6_read_interface(client_config.interface,
  1046. &client_config.ifindex,
  1047. &client6_data.ll_ip6,
  1048. client_config.client_mac)
  1049. ) {
  1050. return 1;
  1051. }
  1052. /* Create client ID based on mac, set clientid_mac_ptr */
  1053. {
  1054. struct d6_option *clientid;
  1055. clientid = xzalloc(2+2+2+2+6);
  1056. clientid->code = D6_OPT_CLIENTID;
  1057. clientid->len = 2+2+6;
  1058. clientid->data[1] = 3; /* DUID-LL */
  1059. clientid->data[3] = 1; /* ethernet */
  1060. clientid_mac_ptr = clientid->data + 2+2;
  1061. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1062. client_config.clientid = (void*)clientid;
  1063. }
  1064. #if !BB_MMU
  1065. /* on NOMMU reexec (i.e., background) early */
  1066. if (!(opt & OPT_f)) {
  1067. bb_daemonize_or_rexec(0 /* flags */, argv);
  1068. logmode = LOGMODE_NONE;
  1069. }
  1070. #endif
  1071. if (opt & OPT_S) {
  1072. openlog(applet_name, LOG_PID, LOG_DAEMON);
  1073. logmode |= LOGMODE_SYSLOG;
  1074. }
  1075. /* Make sure fd 0,1,2 are open */
  1076. bb_sanitize_stdio();
  1077. /* Equivalent of doing a fflush after every \n */
  1078. setlinebuf(stdout);
  1079. /* Create pidfile */
  1080. write_pidfile(client_config.pidfile);
  1081. /* Goes to stdout (unless NOMMU) and possibly syslog */
  1082. bb_error_msg("started, v"BB_VER);
  1083. /* Set up the signal pipe */
  1084. udhcp_sp_setup();
  1085. /* We want random_xid to be random... */
  1086. srand(monotonic_us());
  1087. state = INIT_SELECTING;
  1088. d6_run_script(NULL, "deconfig");
  1089. change_listen_mode(LISTEN_RAW);
  1090. packet_num = 0;
  1091. timeout = 0;
  1092. already_waited_sec = 0;
  1093. /* Main event loop. select() waits on signal pipe and possibly
  1094. * on sockfd.
  1095. * "continue" statements in code below jump to the top of the loop.
  1096. */
  1097. for (;;) {
  1098. int tv;
  1099. struct pollfd pfds[2];
  1100. struct d6_packet packet;
  1101. uint8_t *packet_end;
  1102. /* silence "uninitialized!" warning */
  1103. unsigned timestamp_before_wait = timestamp_before_wait;
  1104. //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
  1105. /* Was opening raw or udp socket here
  1106. * if (listen_mode != LISTEN_NONE && sockfd < 0),
  1107. * but on fast network renew responses return faster
  1108. * than we open sockets. Thus this code is moved
  1109. * to change_listen_mode(). Thus we open listen socket
  1110. * BEFORE we send renew request (see "case BOUND:"). */
  1111. udhcp_sp_fd_set(pfds, sockfd);
  1112. tv = timeout - already_waited_sec;
  1113. retval = 0;
  1114. /* If we already timed out, fall through with retval = 0, else... */
  1115. if (tv > 0) {
  1116. log1("waiting %u seconds", tv);
  1117. timestamp_before_wait = (unsigned)monotonic_sec();
  1118. retval = poll(pfds, 2, tv < INT_MAX/1000 ? tv * 1000 : INT_MAX);
  1119. if (retval < 0) {
  1120. /* EINTR? A signal was caught, don't panic */
  1121. if (errno == EINTR) {
  1122. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1123. continue;
  1124. }
  1125. /* Else: an error occured, panic! */
  1126. bb_perror_msg_and_die("poll");
  1127. }
  1128. }
  1129. /* If timeout dropped to zero, time to become active:
  1130. * resend discover/renew/whatever
  1131. */
  1132. if (retval == 0) {
  1133. /* When running on a bridge, the ifindex may have changed
  1134. * (e.g. if member interfaces were added/removed
  1135. * or if the status of the bridge changed).
  1136. * Refresh ifindex and client_mac:
  1137. */
  1138. if (d6_read_interface(client_config.interface,
  1139. &client_config.ifindex,
  1140. &client6_data.ll_ip6,
  1141. client_config.client_mac)
  1142. ) {
  1143. goto ret0; /* iface is gone? */
  1144. }
  1145. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1146. /* We will restart the wait in any case */
  1147. already_waited_sec = 0;
  1148. switch (state) {
  1149. case INIT_SELECTING:
  1150. if (!discover_retries || packet_num < discover_retries) {
  1151. if (packet_num == 0)
  1152. xid = random_xid();
  1153. /* multicast */
  1154. send_d6_discover(xid, requested_ipv6);
  1155. timeout = discover_timeout;
  1156. packet_num++;
  1157. continue;
  1158. }
  1159. leasefail:
  1160. d6_run_script(NULL, "leasefail");
  1161. #if BB_MMU /* -b is not supported on NOMMU */
  1162. if (opt & OPT_b) { /* background if no lease */
  1163. bb_error_msg("no lease, forking to background");
  1164. client_background();
  1165. /* do not background again! */
  1166. opt = ((opt & ~OPT_b) | OPT_f);
  1167. } else
  1168. #endif
  1169. if (opt & OPT_n) { /* abort if no lease */
  1170. bb_error_msg("no lease, failing");
  1171. retval = 1;
  1172. goto ret;
  1173. }
  1174. /* wait before trying again */
  1175. timeout = tryagain_timeout;
  1176. packet_num = 0;
  1177. continue;
  1178. case REQUESTING:
  1179. if (!discover_retries || packet_num < discover_retries) {
  1180. /* send multicast select packet */
  1181. send_d6_select(xid);
  1182. timeout = discover_timeout;
  1183. packet_num++;
  1184. continue;
  1185. }
  1186. /* Timed out, go back to init state.
  1187. * "discover...select...discover..." loops
  1188. * were seen in the wild. Treat them similarly
  1189. * to "no response to discover" case */
  1190. change_listen_mode(LISTEN_RAW);
  1191. state = INIT_SELECTING;
  1192. goto leasefail;
  1193. case BOUND:
  1194. /* 1/2 lease passed, enter renewing state */
  1195. state = RENEWING;
  1196. client_config.first_secs = 0; /* make secs field count from 0 */
  1197. change_listen_mode(LISTEN_KERNEL);
  1198. log1("entering renew state");
  1199. /* fall right through */
  1200. case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
  1201. case_RENEW_REQUESTED:
  1202. case RENEWING:
  1203. if (timeout > 60) {
  1204. /* send an unicast renew request */
  1205. /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
  1206. * a new UDP socket for sending inside send_renew.
  1207. * I hazard to guess existing listening socket
  1208. * is somehow conflicting with it, but why is it
  1209. * not deterministic then?! Strange.
  1210. * Anyway, it does recover by eventually failing through
  1211. * into INIT_SELECTING state.
  1212. */
  1213. send_d6_renew(xid, &srv6_buf, requested_ipv6);
  1214. timeout >>= 1;
  1215. continue;
  1216. }
  1217. /* Timed out, enter rebinding state */
  1218. log1("entering rebinding state");
  1219. state = REBINDING;
  1220. /* fall right through */
  1221. case REBINDING:
  1222. /* Switch to bcast receive */
  1223. change_listen_mode(LISTEN_RAW);
  1224. /* Lease is *really* about to run out,
  1225. * try to find DHCP server using broadcast */
  1226. if (timeout > 0) {
  1227. /* send a broadcast renew request */
  1228. send_d6_renew(xid, /*server_ipv6:*/ NULL, requested_ipv6);
  1229. timeout >>= 1;
  1230. continue;
  1231. }
  1232. /* Timed out, enter init state */
  1233. bb_error_msg("lease lost, entering init state");
  1234. d6_run_script(NULL, "deconfig");
  1235. state = INIT_SELECTING;
  1236. client_config.first_secs = 0; /* make secs field count from 0 */
  1237. /*timeout = 0; - already is */
  1238. packet_num = 0;
  1239. continue;
  1240. /* case RELEASED: */
  1241. }
  1242. /* yah, I know, *you* say it would never happen */
  1243. timeout = INT_MAX;
  1244. continue; /* back to main loop */
  1245. } /* if select timed out */
  1246. /* select() didn't timeout, something happened */
  1247. /* Is it a signal? */
  1248. /* note: udhcp_sp_read checks poll result before reading */
  1249. switch (udhcp_sp_read(pfds)) {
  1250. case SIGUSR1:
  1251. client_config.first_secs = 0; /* make secs field count from 0 */
  1252. already_waited_sec = 0;
  1253. perform_renew();
  1254. if (state == RENEW_REQUESTED) {
  1255. /* We might be either on the same network
  1256. * (in which case renew might work),
  1257. * or we might be on a completely different one
  1258. * (in which case renew won't ever succeed).
  1259. * For the second case, must make sure timeout
  1260. * is not too big, or else we can send
  1261. * futile renew requests for hours.
  1262. * (Ab)use -A TIMEOUT value (usually 20 sec)
  1263. * as a cap on the timeout.
  1264. */
  1265. if (timeout > tryagain_timeout)
  1266. timeout = tryagain_timeout;
  1267. goto case_RENEW_REQUESTED;
  1268. }
  1269. /* Start things over */
  1270. packet_num = 0;
  1271. /* Kill any timeouts, user wants this to hurry along */
  1272. timeout = 0;
  1273. continue;
  1274. case SIGUSR2:
  1275. perform_d6_release(&srv6_buf, requested_ipv6);
  1276. timeout = INT_MAX;
  1277. continue;
  1278. case SIGTERM:
  1279. bb_error_msg("received %s", "SIGTERM");
  1280. goto ret0;
  1281. }
  1282. /* Is it a packet? */
  1283. if (listen_mode == LISTEN_NONE || !pfds[1].revents)
  1284. continue; /* no */
  1285. {
  1286. int len;
  1287. /* A packet is ready, read it */
  1288. if (listen_mode == LISTEN_KERNEL)
  1289. len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd);
  1290. else
  1291. len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd);
  1292. if (len == -1) {
  1293. /* Error is severe, reopen socket */
  1294. bb_error_msg("read error: %s, reopening socket", strerror(errno));
  1295. sleep(discover_timeout); /* 3 seconds by default */
  1296. change_listen_mode(listen_mode); /* just close and reopen */
  1297. }
  1298. /* If this packet will turn out to be unrelated/bogus,
  1299. * we will go back and wait for next one.
  1300. * Be sure timeout is properly decreased. */
  1301. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1302. if (len < 0)
  1303. continue;
  1304. packet_end = (uint8_t*)&packet + len;
  1305. }
  1306. if ((packet.d6_xid32 & htonl(0x00ffffff)) != xid) {
  1307. log1("xid %x (our is %x), ignoring packet",
  1308. (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)xid);
  1309. continue;
  1310. }
  1311. switch (state) {
  1312. case INIT_SELECTING:
  1313. if (packet.d6_msg_type == D6_MSG_ADVERTISE)
  1314. goto type_is_ok;
  1315. /* DHCPv6 has "Rapid Commit", when instead of Advertise,
  1316. * server sends Reply right away.
  1317. * Fall through to check for this case.
  1318. */
  1319. case REQUESTING:
  1320. case RENEWING:
  1321. case RENEW_REQUESTED:
  1322. case REBINDING:
  1323. if (packet.d6_msg_type == D6_MSG_REPLY) {
  1324. uint32_t lease_seconds;
  1325. struct d6_option *option, *iaaddr;
  1326. type_is_ok:
  1327. option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE);
  1328. if (option && (option->data[0] | option->data[1]) != 0) {
  1329. /* return to init state */
  1330. bb_error_msg("received DHCP NAK (%u)", option->data[4]);
  1331. d6_run_script(&packet, "nak");
  1332. if (state != REQUESTING)
  1333. d6_run_script(NULL, "deconfig");
  1334. change_listen_mode(LISTEN_RAW);
  1335. sleep(3); /* avoid excessive network traffic */
  1336. state = INIT_SELECTING;
  1337. client_config.first_secs = 0; /* make secs field count from 0 */
  1338. requested_ipv6 = NULL;
  1339. timeout = 0;
  1340. packet_num = 0;
  1341. already_waited_sec = 0;
  1342. continue;
  1343. }
  1344. option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID);
  1345. if (!option) {
  1346. bb_error_msg("no server ID, ignoring packet");
  1347. continue;
  1348. /* still selecting - this server looks bad */
  1349. }
  1350. //Note: we do not bother comparing server IDs in Advertise and Reply msgs.
  1351. //server_id variable is used solely for creation of proper server_id option
  1352. //in outgoing packets. (why DHCPv6 even introduced it is a mystery).
  1353. free(client6_data.server_id);
  1354. client6_data.server_id = option;
  1355. if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
  1356. /* enter requesting state */
  1357. state = REQUESTING;
  1358. timeout = 0;
  1359. packet_num = 0;
  1360. already_waited_sec = 0;
  1361. continue;
  1362. }
  1363. /* It's a D6_MSG_REPLY */
  1364. /*
  1365. * RFC 3315 18.1.8. Receipt of Reply Messages
  1366. *
  1367. * Upon the receipt of a valid Reply message in response to a Solicit
  1368. * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or
  1369. * Information-request message, the client extracts the configuration
  1370. * information contained in the Reply. The client MAY choose to report
  1371. * any status code or message from the status code option in the Reply
  1372. * message.
  1373. *
  1374. * The client SHOULD perform duplicate address detection [17] on each of
  1375. * the addresses in any IAs it receives in the Reply message before
  1376. * using that address for traffic. If any of the addresses are found to
  1377. * be in use on the link, the client sends a Decline message to the
  1378. * server as described in section 18.1.7.
  1379. *
  1380. * If the Reply was received in response to a Solicit (with a Rapid
  1381. * Commit option), Request, Renew or Rebind message, the client updates
  1382. * the information it has recorded about IAs from the IA options
  1383. * contained in the Reply message:
  1384. *
  1385. * - Record T1 and T2 times.
  1386. *
  1387. * - Add any new addresses in the IA option to the IA as recorded by
  1388. * the client.
  1389. *
  1390. * - Update lifetimes for any addresses in the IA option that the
  1391. * client already has recorded in the IA.
  1392. *
  1393. * - Discard any addresses from the IA, as recorded by the client, that
  1394. * have a valid lifetime of 0 in the IA Address option.
  1395. *
  1396. * - Leave unchanged any information about addresses the client has
  1397. * recorded in the IA but that were not included in the IA from the
  1398. * server.
  1399. *
  1400. * Management of the specific configuration information is detailed in
  1401. * the definition of each option in section 22.
  1402. *
  1403. * If the client receives a Reply message with a Status Code containing
  1404. * UnspecFail, the server is indicating that it was unable to process
  1405. * the message due to an unspecified failure condition. If the client
  1406. * retransmits the original message to the same server to retry the
  1407. * desired operation, the client MUST limit the rate at which it
  1408. * retransmits the message and limit the duration of the time during
  1409. * which it retransmits the message.
  1410. *
  1411. * When the client receives a Reply message with a Status Code option
  1412. * with the value UseMulticast, the client records the receipt of the
  1413. * message and sends subsequent messages to the server through the
  1414. * interface on which the message was received using multicast. The
  1415. * client resends the original message using multicast.
  1416. *
  1417. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1418. * | OPTION_IA_NA | option-len |
  1419. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1420. * | IAID (4 octets) |
  1421. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1422. * | T1 |
  1423. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1424. * | T2 |
  1425. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1426. * | |
  1427. * . IA_NA-options .
  1428. * . .
  1429. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1430. *
  1431. *
  1432. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1433. * | OPTION_IAADDR | option-len |
  1434. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1435. * | |
  1436. * | IPv6 address |
  1437. * | |
  1438. * | |
  1439. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1440. * | preferred-lifetime |
  1441. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1442. * | valid-lifetime |
  1443. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1444. * . .
  1445. * . IAaddr-options .
  1446. * . .
  1447. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1448. */
  1449. free(client6_data.ia_na);
  1450. client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA);
  1451. if (!client6_data.ia_na) {
  1452. bb_error_msg("no %s option, ignoring packet", "IA_NA");
  1453. continue;
  1454. }
  1455. if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) {
  1456. bb_error_msg("IA_NA option is too short:%d bytes", client6_data.ia_na->len);
  1457. continue;
  1458. }
  1459. iaaddr = d6_find_option(client6_data.ia_na->data + 4 + 4 + 4,
  1460. client6_data.ia_na->data + client6_data.ia_na->len,
  1461. D6_OPT_IAADDR
  1462. );
  1463. if (!iaaddr) {
  1464. bb_error_msg("no %s option, ignoring packet", "IAADDR");
  1465. continue;
  1466. }
  1467. if (iaaddr->len < (16 + 4 + 4)) {
  1468. bb_error_msg("IAADDR option is too short:%d bytes", iaaddr->len);
  1469. continue;
  1470. }
  1471. /* Note: the address is sufficiently aligned for cast:
  1472. * we _copied_ IA-NA, and copy is always well-aligned.
  1473. */
  1474. requested_ipv6 = (struct in6_addr*) iaaddr->data;
  1475. move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4);
  1476. lease_seconds = ntohl(lease_seconds);
  1477. /* paranoia: must not be too small and not prone to overflows */
  1478. if (lease_seconds < 0x10)
  1479. lease_seconds = 0x10;
  1480. /// TODO: check for 0 lease time?
  1481. if (lease_seconds > 0x7fffffff / 1000)
  1482. lease_seconds = 0x7fffffff / 1000;
  1483. /* enter bound state */
  1484. timeout = lease_seconds / 2;
  1485. bb_error_msg("lease obtained, lease time %u",
  1486. /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
  1487. d6_run_script(&packet, state == REQUESTING ? "bound" : "renew");
  1488. state = BOUND;
  1489. change_listen_mode(LISTEN_NONE);
  1490. if (opt & OPT_q) { /* quit after lease */
  1491. goto ret0;
  1492. }
  1493. /* future renew failures should not exit (JM) */
  1494. opt &= ~OPT_n;
  1495. #if BB_MMU /* NOMMU case backgrounded earlier */
  1496. if (!(opt & OPT_f)) {
  1497. client_background();
  1498. /* do not background again! */
  1499. opt = ((opt & ~OPT_b) | OPT_f);
  1500. }
  1501. #endif
  1502. already_waited_sec = 0;
  1503. continue; /* back to main loop */
  1504. }
  1505. continue;
  1506. /* case BOUND: - ignore all packets */
  1507. /* case RELEASED: - ignore all packets */
  1508. }
  1509. /* back to main loop */
  1510. } /* for (;;) - main loop ends */
  1511. ret0:
  1512. if (opt & OPT_R) /* release on quit */
  1513. perform_d6_release(&srv6_buf, requested_ipv6);
  1514. retval = 0;
  1515. ret:
  1516. /*if (client_config.pidfile) - remove_pidfile has its own check */
  1517. remove_pidfile(client_config.pidfile);
  1518. return retval;
  1519. }