d6_dhcpc.c 58 KB

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