d6_dhcpc.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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("ipv6prefix_lease=%u", (unsigned)v32);
  260. sprint_nip6(ipv6str, option + 4 + 4 + 4 + 1);
  261. *new_env() = xasprintf("ipv6prefix=%s/%u", ipv6str, (unsigned)(option[4 + 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. /* TODO? send_flags: MSG_DONTROUTE (see IPv4 code for reason why) */
  635. );
  636. }
  637. return d6_mcast_from_client_config_ifindex(&packet, opt_ptr);
  638. }
  639. /* Unicast a DHCP release message */
  640. static int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
  641. {
  642. struct d6_packet packet;
  643. uint8_t *opt_ptr;
  644. /* Fill in: msg type, client id */
  645. opt_ptr = init_d6_packet(&packet, D6_MSG_RELEASE, random_xid());
  646. /* server id */
  647. opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
  648. /* IA NA (contains our current IP) */
  649. opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
  650. bb_error_msg("sending %s", "release");
  651. return d6_send_kernel_packet(
  652. &packet, (opt_ptr - (uint8_t*) &packet),
  653. our_cur_ipv6, CLIENT_PORT6,
  654. server_ipv6, SERVER_PORT6,
  655. client_config.ifindex
  656. );
  657. }
  658. /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
  659. /* NOINLINE: limit stack usage in caller */
  660. static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6, struct d6_packet *d6_pkt, int fd)
  661. {
  662. int bytes;
  663. struct ip6_udp_d6_packet packet;
  664. bytes = safe_read(fd, &packet, sizeof(packet));
  665. if (bytes < 0) {
  666. log1("packet read error, ignoring");
  667. /* NB: possible down interface, etc. Caller should pause. */
  668. return bytes; /* returns -1 */
  669. }
  670. if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) {
  671. log1("packet is too short, ignoring");
  672. return -2;
  673. }
  674. if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) {
  675. /* packet is bigger than sizeof(packet), we did partial read */
  676. log1("oversized packet, ignoring");
  677. return -2;
  678. }
  679. /* ignore any extra garbage bytes */
  680. bytes = sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen);
  681. /* make sure its the right packet for us, and that it passes sanity checks */
  682. if (packet.ip6.ip6_nxt != IPPROTO_UDP
  683. || (packet.ip6.ip6_vfc >> 4) != 6
  684. || packet.udp.dest != htons(CLIENT_PORT6)
  685. /* || bytes > (int) sizeof(packet) - can't happen */
  686. || packet.udp.len != packet.ip6.ip6_plen
  687. ) {
  688. log1("unrelated/bogus packet, ignoring");
  689. return -2;
  690. }
  691. //How to do this for ipv6?
  692. // /* verify UDP checksum. IP header has to be modified for this */
  693. // memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
  694. // /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
  695. // packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
  696. // check = packet.udp.check;
  697. // packet.udp.check = 0;
  698. // if (check && check != inet_cksum((uint16_t *)&packet, bytes)) {
  699. // log1("packet with bad UDP checksum received, ignoring");
  700. // return -2;
  701. // }
  702. if (peer_ipv6)
  703. *peer_ipv6 = packet.ip6.ip6_src; /* struct copy */
  704. log1("received %s", "a packet");
  705. d6_dump_packet(&packet.data);
  706. bytes -= sizeof(packet.ip6) + sizeof(packet.udp);
  707. memcpy(d6_pkt, &packet.data, bytes);
  708. return bytes;
  709. }
  710. /*** Main ***/
  711. static int sockfd = -1;
  712. #define LISTEN_NONE 0
  713. #define LISTEN_KERNEL 1
  714. #define LISTEN_RAW 2
  715. static smallint listen_mode;
  716. /* initial state: (re)start DHCP negotiation */
  717. #define INIT_SELECTING 0
  718. /* discover was sent, DHCPOFFER reply received */
  719. #define REQUESTING 1
  720. /* select/renew was sent, DHCPACK reply received */
  721. #define BOUND 2
  722. /* half of lease passed, want to renew it by sending unicast renew requests */
  723. #define RENEWING 3
  724. /* renew requests were not answered, lease is almost over, send broadcast renew */
  725. #define REBINDING 4
  726. /* manually requested renew (SIGUSR1) */
  727. #define RENEW_REQUESTED 5
  728. /* release, possibly manually requested (SIGUSR2) */
  729. #define RELEASED 6
  730. static smallint state;
  731. static int d6_raw_socket(int ifindex)
  732. {
  733. int fd;
  734. struct sockaddr_ll sock;
  735. /*
  736. * Comment:
  737. *
  738. * I've selected not to see LL header, so BPF doesn't see it, too.
  739. * The filter may also pass non-IP and non-ARP packets, but we do
  740. * a more complete check when receiving the message in userspace.
  741. *
  742. * and filter shamelessly stolen from:
  743. *
  744. * http://www.flamewarmaster.de/software/dhcpclient/
  745. *
  746. * There are a few other interesting ideas on that page (look under
  747. * "Motivation"). Use of netlink events is most interesting. Think
  748. * of various network servers listening for events and reconfiguring.
  749. * That would obsolete sending HUP signals and/or make use of restarts.
  750. *
  751. * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
  752. * License: GPL v2.
  753. *
  754. * TODO: make conditional?
  755. */
  756. #if 0
  757. static const struct sock_filter filter_instr[] = {
  758. /* load 9th byte (protocol) */
  759. BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
  760. /* jump to L1 if it is IPPROTO_UDP, else to L4 */
  761. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
  762. /* L1: load halfword from offset 6 (flags and frag offset) */
  763. BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
  764. /* jump to L4 if any bits in frag offset field are set, else to L2 */
  765. BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
  766. /* L2: skip IP header (load index reg with header len) */
  767. BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0),
  768. /* load udp destination port from halfword[header_len + 2] */
  769. BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
  770. /* jump to L3 if udp dport is CLIENT_PORT6, else to L4 */
  771. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
  772. /* L3: accept packet */
  773. BPF_STMT(BPF_RET|BPF_K, 0x7fffffff),
  774. /* L4: discard packet */
  775. BPF_STMT(BPF_RET|BPF_K, 0),
  776. };
  777. static const struct sock_fprog filter_prog = {
  778. .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
  779. /* casting const away: */
  780. .filter = (struct sock_filter *) filter_instr,
  781. };
  782. #endif
  783. log2("opening raw socket on ifindex %d", ifindex);
  784. fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  785. log2("got raw socket fd %d", fd);
  786. memset(&sock, 0, sizeof(sock)); /* let's be deterministic */
  787. sock.sll_family = AF_PACKET;
  788. sock.sll_protocol = htons(ETH_P_IPV6);
  789. sock.sll_ifindex = ifindex;
  790. /*sock.sll_hatype = ARPHRD_???;*/
  791. /*sock.sll_pkttype = PACKET_???;*/
  792. /*sock.sll_halen = ???;*/
  793. /*sock.sll_addr[8] = ???;*/
  794. xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
  795. #if 0
  796. if (CLIENT_PORT6 == 546) {
  797. /* Use only if standard port is in use */
  798. /* Ignoring error (kernel may lack support for this) */
  799. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
  800. sizeof(filter_prog)) >= 0)
  801. log1("attached filter to raw socket fd %d", fd); // log?
  802. }
  803. #endif
  804. log1("created raw socket");
  805. return fd;
  806. }
  807. static void change_listen_mode(int new_mode)
  808. {
  809. log1("entering listen mode: %s",
  810. new_mode != LISTEN_NONE
  811. ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw")
  812. : "none"
  813. );
  814. listen_mode = new_mode;
  815. if (sockfd >= 0) {
  816. close(sockfd);
  817. sockfd = -1;
  818. }
  819. if (new_mode == LISTEN_KERNEL)
  820. sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface);
  821. else if (new_mode != LISTEN_NONE)
  822. sockfd = d6_raw_socket(client_config.ifindex);
  823. /* else LISTEN_NONE: sockfd stays closed */
  824. }
  825. /* Called only on SIGUSR1 */
  826. static void perform_renew(void)
  827. {
  828. bb_error_msg("performing DHCP renew");
  829. switch (state) {
  830. case BOUND:
  831. change_listen_mode(LISTEN_KERNEL);
  832. case RENEWING:
  833. case REBINDING:
  834. state = RENEW_REQUESTED;
  835. break;
  836. case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
  837. d6_run_script(NULL, "deconfig");
  838. case REQUESTING:
  839. case RELEASED:
  840. change_listen_mode(LISTEN_RAW);
  841. state = INIT_SELECTING;
  842. break;
  843. case INIT_SELECTING:
  844. break;
  845. }
  846. }
  847. static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
  848. {
  849. /* send release packet */
  850. if (state == BOUND
  851. || state == RENEWING
  852. || state == REBINDING
  853. || state == RENEW_REQUESTED
  854. ) {
  855. bb_error_msg("unicasting a release");
  856. send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */
  857. }
  858. bb_error_msg("entering released state");
  859. /*
  860. * We can be here on: SIGUSR2,
  861. * or on exit (SIGTERM) and -R "release on quit" is specified.
  862. * Users requested to be notified in all cases, even if not in one
  863. * of the states above.
  864. */
  865. d6_run_script(NULL, "deconfig");
  866. change_listen_mode(LISTEN_NONE);
  867. state = RELEASED;
  868. }
  869. ///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
  870. ///{
  871. /// uint8_t *storage;
  872. /// int len = strnlen(str, 255);
  873. /// storage = xzalloc(len + extra + OPT_DATA);
  874. /// storage[OPT_CODE] = code;
  875. /// storage[OPT_LEN] = len + extra;
  876. /// memcpy(storage + extra + OPT_DATA, str, len);
  877. /// return storage;
  878. ///}
  879. #if BB_MMU
  880. static void client_background(void)
  881. {
  882. bb_daemonize(0);
  883. logmode &= ~LOGMODE_STDIO;
  884. /* rewrite pidfile, as our pid is different now */
  885. write_pidfile(client_config.pidfile);
  886. }
  887. #endif
  888. //usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  889. //usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__
  890. //usage:#else
  891. //usage:# define IF_UDHCP_VERBOSE(...)
  892. //usage:#endif
  893. //usage:#define udhcpc6_trivial_usage
  894. //usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"oR] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n"
  895. //usage: " [-x OPT:VAL]... [-O OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]")
  896. //usage:#define udhcpc6_full_usage "\n"
  897. //usage: IF_LONG_OPTS(
  898. //usage: "\n -i,--interface IFACE Interface to use (default eth0)"
  899. //usage: "\n -p,--pidfile FILE Create pidfile"
  900. //usage: "\n -s,--script PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
  901. //usage: "\n -B,--broadcast Request broadcast replies"
  902. //usage: "\n -t,--retries N Send up to N discover packets"
  903. //usage: "\n -T,--timeout N Pause between packets (default 3 seconds)"
  904. //usage: "\n -A,--tryagain N Wait N seconds after failure (default 20)"
  905. //usage: "\n -f,--foreground Run in foreground"
  906. //usage: USE_FOR_MMU(
  907. //usage: "\n -b,--background Background if lease is not obtained"
  908. //usage: )
  909. //usage: "\n -n,--now Exit if lease is not obtained"
  910. //usage: "\n -q,--quit Exit after obtaining lease"
  911. //usage: "\n -R,--release Release IP on exit"
  912. //usage: "\n -S,--syslog Log to syslog too"
  913. //usage: IF_FEATURE_UDHCP_PORT(
  914. //usage: "\n -P,--client-port N Use port N (default 546)"
  915. //usage: )
  916. ////usage: IF_FEATURE_UDHCPC_ARPING(
  917. ////usage: "\n -a,--arping Use arping to validate offered address"
  918. ////usage: )
  919. //usage: "\n -O,--request-option OPT Request option OPT from server (cumulative)"
  920. //usage: "\n -o,--no-default-options Don't request any options (unless -O is given)"
  921. //usage: "\n -r,--request IP Request this IP address"
  922. //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
  923. //usage: "\n Examples of string, numeric, and hex byte opts:"
  924. //usage: "\n -x hostname:bbox - option 12"
  925. //usage: "\n -x lease:3600 - option 51 (lease time)"
  926. //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
  927. //usage: IF_UDHCP_VERBOSE(
  928. //usage: "\n -v Verbose"
  929. //usage: )
  930. //usage: )
  931. //usage: IF_NOT_LONG_OPTS(
  932. //usage: "\n -i IFACE Interface to use (default eth0)"
  933. //usage: "\n -p FILE Create pidfile"
  934. //usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
  935. //usage: "\n -B Request broadcast replies"
  936. //usage: "\n -t N Send up to N discover packets"
  937. //usage: "\n -T N Pause between packets (default 3 seconds)"
  938. //usage: "\n -A N Wait N seconds (default 20) after failure"
  939. //usage: "\n -f Run in foreground"
  940. //usage: USE_FOR_MMU(
  941. //usage: "\n -b Background if lease is not obtained"
  942. //usage: )
  943. //usage: "\n -n Exit if lease is not obtained"
  944. //usage: "\n -q Exit after obtaining lease"
  945. //usage: "\n -R Release IP on exit"
  946. //usage: "\n -S Log to syslog too"
  947. //usage: IF_FEATURE_UDHCP_PORT(
  948. //usage: "\n -P N Use port N (default 546)"
  949. //usage: )
  950. ////usage: IF_FEATURE_UDHCPC_ARPING(
  951. ////usage: "\n -a Use arping to validate offered address"
  952. ////usage: )
  953. //usage: "\n -O OPT Request option OPT from server (cumulative)"
  954. //usage: "\n -o Don't request any options (unless -O is given)"
  955. //usage: "\n -r IP Request this IP address"
  956. //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
  957. //usage: "\n Examples of string, numeric, and hex byte opts:"
  958. //usage: "\n -x hostname:bbox - option 12"
  959. //usage: "\n -x lease:3600 - option 51 (lease time)"
  960. //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
  961. //usage: IF_UDHCP_VERBOSE(
  962. //usage: "\n -v Verbose"
  963. //usage: )
  964. //usage: )
  965. //usage: "\nSignals:"
  966. //usage: "\n USR1 Renew lease"
  967. //usage: "\n USR2 Release lease"
  968. int udhcpc6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  969. int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
  970. {
  971. const char *str_r;
  972. IF_FEATURE_UDHCP_PORT(char *str_P;)
  973. void *clientid_mac_ptr;
  974. llist_t *list_O = NULL;
  975. llist_t *list_x = NULL;
  976. int tryagain_timeout = 20;
  977. int discover_timeout = 3;
  978. int discover_retries = 3;
  979. struct in6_addr srv6_buf;
  980. struct in6_addr ipv6_buf;
  981. struct in6_addr *requested_ipv6;
  982. uint32_t xid = 0;
  983. int packet_num;
  984. int timeout; /* must be signed */
  985. unsigned already_waited_sec;
  986. unsigned opt;
  987. int retval;
  988. setup_common_bufsiz();
  989. /* Default options */
  990. IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;)
  991. IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;)
  992. client_config.interface = "eth0";
  993. client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
  994. /* Parse command line */
  995. opt = getopt32long(argv, "^"
  996. /* O,x: list; -T,-t,-A take numeric param */
  997. "i:np:qRr:s:T:+t:+SA:+O:*ox:*f"
  998. USE_FOR_MMU("b")
  999. ///IF_FEATURE_UDHCPC_ARPING("a")
  1000. IF_FEATURE_UDHCP_PORT("P:")
  1001. "v"
  1002. "\0" IF_UDHCP_VERBOSE("vv") /* -v is a counter */
  1003. , udhcpc6_longopts
  1004. , &client_config.interface, &client_config.pidfile, &str_r /* i,p */
  1005. , &client_config.script /* s */
  1006. , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
  1007. , &list_O
  1008. , &list_x
  1009. IF_FEATURE_UDHCP_PORT(, &str_P)
  1010. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  1011. );
  1012. requested_ipv6 = NULL;
  1013. if (opt & OPT_r) {
  1014. if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0)
  1015. bb_error_msg_and_die("bad IPv6 address '%s'", str_r);
  1016. requested_ipv6 = &ipv6_buf;
  1017. }
  1018. #if ENABLE_FEATURE_UDHCP_PORT
  1019. if (opt & OPT_P) {
  1020. CLIENT_PORT6 = xatou16(str_P);
  1021. SERVER_PORT6 = CLIENT_PORT6 + 1;
  1022. }
  1023. #endif
  1024. while (list_O) {
  1025. char *optstr = llist_pop(&list_O);
  1026. unsigned n = bb_strtou(optstr, NULL, 0);
  1027. if (errno || n > 254) {
  1028. n = udhcp_option_idx(optstr, d6_option_strings);
  1029. n = d6_optflags[n].code;
  1030. }
  1031. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1032. }
  1033. if (!(opt & OPT_o)) {
  1034. unsigned i, n;
  1035. for (i = 0; (n = d6_optflags[i].code) != 0; i++) {
  1036. if (d6_optflags[i].flags & OPTION_REQ) {
  1037. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  1038. }
  1039. }
  1040. }
  1041. while (list_x) {
  1042. char *optstr = llist_pop(&list_x);
  1043. char *colon = strchr(optstr, ':');
  1044. if (colon)
  1045. *colon = ' ';
  1046. /* now it looks similar to udhcpd's config file line:
  1047. * "optname optval", using the common routine: */
  1048. udhcp_str2optset(optstr, &client_config.options, d6_optflags, d6_option_strings);
  1049. if (colon)
  1050. *colon = ':'; /* restore it for NOMMU reexec */
  1051. }
  1052. if (d6_read_interface(client_config.interface,
  1053. &client_config.ifindex,
  1054. &client6_data.ll_ip6,
  1055. client_config.client_mac)
  1056. ) {
  1057. return 1;
  1058. }
  1059. /* Create client ID based on mac, set clientid_mac_ptr */
  1060. {
  1061. struct d6_option *clientid;
  1062. clientid = xzalloc(2+2+2+2+6);
  1063. clientid->code = D6_OPT_CLIENTID;
  1064. clientid->len = 2+2+6;
  1065. clientid->data[1] = 3; /* DUID-LL */
  1066. clientid->data[3] = 1; /* ethernet */
  1067. clientid_mac_ptr = clientid->data + 2+2;
  1068. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1069. client_config.clientid = (void*)clientid;
  1070. }
  1071. #if !BB_MMU
  1072. /* on NOMMU reexec (i.e., background) early */
  1073. if (!(opt & OPT_f)) {
  1074. bb_daemonize_or_rexec(0 /* flags */, argv);
  1075. logmode = LOGMODE_NONE;
  1076. }
  1077. #endif
  1078. if (opt & OPT_S) {
  1079. openlog(applet_name, LOG_PID, LOG_DAEMON);
  1080. logmode |= LOGMODE_SYSLOG;
  1081. }
  1082. /* Make sure fd 0,1,2 are open */
  1083. bb_sanitize_stdio();
  1084. /* Equivalent of doing a fflush after every \n */
  1085. setlinebuf(stdout);
  1086. /* Create pidfile */
  1087. write_pidfile(client_config.pidfile);
  1088. /* Goes to stdout (unless NOMMU) and possibly syslog */
  1089. bb_error_msg("started, v"BB_VER);
  1090. /* Set up the signal pipe */
  1091. udhcp_sp_setup();
  1092. /* We want random_xid to be random... */
  1093. srand(monotonic_us());
  1094. state = INIT_SELECTING;
  1095. d6_run_script(NULL, "deconfig");
  1096. change_listen_mode(LISTEN_RAW);
  1097. packet_num = 0;
  1098. timeout = 0;
  1099. already_waited_sec = 0;
  1100. /* Main event loop. select() waits on signal pipe and possibly
  1101. * on sockfd.
  1102. * "continue" statements in code below jump to the top of the loop.
  1103. */
  1104. for (;;) {
  1105. int tv;
  1106. struct pollfd pfds[2];
  1107. struct d6_packet packet;
  1108. uint8_t *packet_end;
  1109. /* silence "uninitialized!" warning */
  1110. unsigned timestamp_before_wait = timestamp_before_wait;
  1111. //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode);
  1112. /* Was opening raw or udp socket here
  1113. * if (listen_mode != LISTEN_NONE && sockfd < 0),
  1114. * but on fast network renew responses return faster
  1115. * than we open sockets. Thus this code is moved
  1116. * to change_listen_mode(). Thus we open listen socket
  1117. * BEFORE we send renew request (see "case BOUND:"). */
  1118. udhcp_sp_fd_set(pfds, sockfd);
  1119. tv = timeout - already_waited_sec;
  1120. retval = 0;
  1121. /* If we already timed out, fall through with retval = 0, else... */
  1122. if (tv > 0) {
  1123. log1("waiting %u seconds", tv);
  1124. timestamp_before_wait = (unsigned)monotonic_sec();
  1125. retval = poll(pfds, 2, tv < INT_MAX/1000 ? tv * 1000 : INT_MAX);
  1126. if (retval < 0) {
  1127. /* EINTR? A signal was caught, don't panic */
  1128. if (errno == EINTR) {
  1129. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1130. continue;
  1131. }
  1132. /* Else: an error occured, panic! */
  1133. bb_perror_msg_and_die("poll");
  1134. }
  1135. }
  1136. /* If timeout dropped to zero, time to become active:
  1137. * resend discover/renew/whatever
  1138. */
  1139. if (retval == 0) {
  1140. /* When running on a bridge, the ifindex may have changed
  1141. * (e.g. if member interfaces were added/removed
  1142. * or if the status of the bridge changed).
  1143. * Refresh ifindex and client_mac:
  1144. */
  1145. if (d6_read_interface(client_config.interface,
  1146. &client_config.ifindex,
  1147. &client6_data.ll_ip6,
  1148. client_config.client_mac)
  1149. ) {
  1150. goto ret0; /* iface is gone? */
  1151. }
  1152. memcpy(clientid_mac_ptr, client_config.client_mac, 6);
  1153. /* We will restart the wait in any case */
  1154. already_waited_sec = 0;
  1155. switch (state) {
  1156. case INIT_SELECTING:
  1157. if (!discover_retries || packet_num < discover_retries) {
  1158. if (packet_num == 0)
  1159. xid = random_xid();
  1160. /* multicast */
  1161. send_d6_discover(xid, requested_ipv6);
  1162. timeout = discover_timeout;
  1163. packet_num++;
  1164. continue;
  1165. }
  1166. leasefail:
  1167. d6_run_script(NULL, "leasefail");
  1168. #if BB_MMU /* -b is not supported on NOMMU */
  1169. if (opt & OPT_b) { /* background if no lease */
  1170. bb_error_msg("no lease, forking to background");
  1171. client_background();
  1172. /* do not background again! */
  1173. opt = ((opt & ~OPT_b) | OPT_f);
  1174. } else
  1175. #endif
  1176. if (opt & OPT_n) { /* abort if no lease */
  1177. bb_error_msg("no lease, failing");
  1178. retval = 1;
  1179. goto ret;
  1180. }
  1181. /* wait before trying again */
  1182. timeout = tryagain_timeout;
  1183. packet_num = 0;
  1184. continue;
  1185. case REQUESTING:
  1186. if (!discover_retries || packet_num < discover_retries) {
  1187. /* send multicast select packet */
  1188. send_d6_select(xid);
  1189. timeout = discover_timeout;
  1190. packet_num++;
  1191. continue;
  1192. }
  1193. /* Timed out, go back to init state.
  1194. * "discover...select...discover..." loops
  1195. * were seen in the wild. Treat them similarly
  1196. * to "no response to discover" case */
  1197. change_listen_mode(LISTEN_RAW);
  1198. state = INIT_SELECTING;
  1199. goto leasefail;
  1200. case BOUND:
  1201. /* 1/2 lease passed, enter renewing state */
  1202. state = RENEWING;
  1203. client_config.first_secs = 0; /* make secs field count from 0 */
  1204. change_listen_mode(LISTEN_KERNEL);
  1205. log1("entering renew state");
  1206. /* fall right through */
  1207. case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
  1208. case_RENEW_REQUESTED:
  1209. case RENEWING:
  1210. if (timeout > 60) {
  1211. /* send an unicast renew request */
  1212. /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
  1213. * a new UDP socket for sending inside send_renew.
  1214. * I hazard to guess existing listening socket
  1215. * is somehow conflicting with it, but why is it
  1216. * not deterministic then?! Strange.
  1217. * Anyway, it does recover by eventually failing through
  1218. * into INIT_SELECTING state.
  1219. */
  1220. send_d6_renew(xid, &srv6_buf, requested_ipv6);
  1221. timeout >>= 1;
  1222. continue;
  1223. }
  1224. /* Timed out, enter rebinding state */
  1225. log1("entering rebinding state");
  1226. state = REBINDING;
  1227. /* fall right through */
  1228. case REBINDING:
  1229. /* Switch to bcast receive */
  1230. change_listen_mode(LISTEN_RAW);
  1231. /* Lease is *really* about to run out,
  1232. * try to find DHCP server using broadcast */
  1233. if (timeout > 0) {
  1234. /* send a broadcast renew request */
  1235. send_d6_renew(xid, /*server_ipv6:*/ NULL, requested_ipv6);
  1236. timeout >>= 1;
  1237. continue;
  1238. }
  1239. /* Timed out, enter init state */
  1240. bb_error_msg("lease lost, entering init state");
  1241. d6_run_script(NULL, "deconfig");
  1242. state = INIT_SELECTING;
  1243. client_config.first_secs = 0; /* make secs field count from 0 */
  1244. /*timeout = 0; - already is */
  1245. packet_num = 0;
  1246. continue;
  1247. /* case RELEASED: */
  1248. }
  1249. /* yah, I know, *you* say it would never happen */
  1250. timeout = INT_MAX;
  1251. continue; /* back to main loop */
  1252. } /* if poll timed out */
  1253. /* poll() didn't timeout, something happened */
  1254. /* Is it a signal? */
  1255. switch (udhcp_sp_read()) {
  1256. case SIGUSR1:
  1257. client_config.first_secs = 0; /* make secs field count from 0 */
  1258. already_waited_sec = 0;
  1259. perform_renew();
  1260. if (state == RENEW_REQUESTED) {
  1261. /* We might be either on the same network
  1262. * (in which case renew might work),
  1263. * or we might be on a completely different one
  1264. * (in which case renew won't ever succeed).
  1265. * For the second case, must make sure timeout
  1266. * is not too big, or else we can send
  1267. * futile renew requests for hours.
  1268. * (Ab)use -A TIMEOUT value (usually 20 sec)
  1269. * as a cap on the timeout.
  1270. */
  1271. if (timeout > tryagain_timeout)
  1272. timeout = tryagain_timeout;
  1273. goto case_RENEW_REQUESTED;
  1274. }
  1275. /* Start things over */
  1276. packet_num = 0;
  1277. /* Kill any timeouts, user wants this to hurry along */
  1278. timeout = 0;
  1279. continue;
  1280. case SIGUSR2:
  1281. perform_d6_release(&srv6_buf, requested_ipv6);
  1282. timeout = INT_MAX;
  1283. continue;
  1284. case SIGTERM:
  1285. bb_error_msg("received %s", "SIGTERM");
  1286. goto ret0;
  1287. }
  1288. /* Is it a packet? */
  1289. if (!pfds[1].revents)
  1290. continue; /* no */
  1291. {
  1292. int len;
  1293. /* A packet is ready, read it */
  1294. if (listen_mode == LISTEN_KERNEL)
  1295. len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd);
  1296. else
  1297. len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd);
  1298. if (len == -1) {
  1299. /* Error is severe, reopen socket */
  1300. bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
  1301. sleep(discover_timeout); /* 3 seconds by default */
  1302. change_listen_mode(listen_mode); /* just close and reopen */
  1303. }
  1304. /* If this packet will turn out to be unrelated/bogus,
  1305. * we will go back and wait for next one.
  1306. * Be sure timeout is properly decreased. */
  1307. already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
  1308. if (len < 0)
  1309. continue;
  1310. packet_end = (uint8_t*)&packet + len;
  1311. }
  1312. if ((packet.d6_xid32 & htonl(0x00ffffff)) != xid) {
  1313. log1("xid %x (our is %x), ignoring packet",
  1314. (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)xid);
  1315. continue;
  1316. }
  1317. switch (state) {
  1318. case INIT_SELECTING:
  1319. if (packet.d6_msg_type == D6_MSG_ADVERTISE)
  1320. goto type_is_ok;
  1321. /* DHCPv6 has "Rapid Commit", when instead of Advertise,
  1322. * server sends Reply right away.
  1323. * Fall through to check for this case.
  1324. */
  1325. case REQUESTING:
  1326. case RENEWING:
  1327. case RENEW_REQUESTED:
  1328. case REBINDING:
  1329. if (packet.d6_msg_type == D6_MSG_REPLY) {
  1330. uint32_t lease_seconds;
  1331. struct d6_option *option, *iaaddr;
  1332. type_is_ok:
  1333. option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE);
  1334. if (option && (option->data[0] | option->data[1]) != 0) {
  1335. /* return to init state */
  1336. bb_error_msg("received DHCP NAK (%u)", option->data[4]);
  1337. d6_run_script(&packet, "nak");
  1338. if (state != REQUESTING)
  1339. d6_run_script(NULL, "deconfig");
  1340. change_listen_mode(LISTEN_RAW);
  1341. sleep(3); /* avoid excessive network traffic */
  1342. state = INIT_SELECTING;
  1343. client_config.first_secs = 0; /* make secs field count from 0 */
  1344. requested_ipv6 = NULL;
  1345. timeout = 0;
  1346. packet_num = 0;
  1347. already_waited_sec = 0;
  1348. continue;
  1349. }
  1350. option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID);
  1351. if (!option) {
  1352. bb_error_msg("no server ID, ignoring packet");
  1353. continue;
  1354. /* still selecting - this server looks bad */
  1355. }
  1356. //Note: we do not bother comparing server IDs in Advertise and Reply msgs.
  1357. //server_id variable is used solely for creation of proper server_id option
  1358. //in outgoing packets. (why DHCPv6 even introduced it is a mystery).
  1359. free(client6_data.server_id);
  1360. client6_data.server_id = option;
  1361. if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
  1362. /* enter requesting state */
  1363. state = REQUESTING;
  1364. timeout = 0;
  1365. packet_num = 0;
  1366. already_waited_sec = 0;
  1367. continue;
  1368. }
  1369. /* It's a D6_MSG_REPLY */
  1370. /*
  1371. * RFC 3315 18.1.8. Receipt of Reply Messages
  1372. *
  1373. * Upon the receipt of a valid Reply message in response to a Solicit
  1374. * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or
  1375. * Information-request message, the client extracts the configuration
  1376. * information contained in the Reply. The client MAY choose to report
  1377. * any status code or message from the status code option in the Reply
  1378. * message.
  1379. *
  1380. * The client SHOULD perform duplicate address detection [17] on each of
  1381. * the addresses in any IAs it receives in the Reply message before
  1382. * using that address for traffic. If any of the addresses are found to
  1383. * be in use on the link, the client sends a Decline message to the
  1384. * server as described in section 18.1.7.
  1385. *
  1386. * If the Reply was received in response to a Solicit (with a Rapid
  1387. * Commit option), Request, Renew or Rebind message, the client updates
  1388. * the information it has recorded about IAs from the IA options
  1389. * contained in the Reply message:
  1390. *
  1391. * - Record T1 and T2 times.
  1392. *
  1393. * - Add any new addresses in the IA option to the IA as recorded by
  1394. * the client.
  1395. *
  1396. * - Update lifetimes for any addresses in the IA option that the
  1397. * client already has recorded in the IA.
  1398. *
  1399. * - Discard any addresses from the IA, as recorded by the client, that
  1400. * have a valid lifetime of 0 in the IA Address option.
  1401. *
  1402. * - Leave unchanged any information about addresses the client has
  1403. * recorded in the IA but that were not included in the IA from the
  1404. * server.
  1405. *
  1406. * Management of the specific configuration information is detailed in
  1407. * the definition of each option in section 22.
  1408. *
  1409. * If the client receives a Reply message with a Status Code containing
  1410. * UnspecFail, the server is indicating that it was unable to process
  1411. * the message due to an unspecified failure condition. If the client
  1412. * retransmits the original message to the same server to retry the
  1413. * desired operation, the client MUST limit the rate at which it
  1414. * retransmits the message and limit the duration of the time during
  1415. * which it retransmits the message.
  1416. *
  1417. * When the client receives a Reply message with a Status Code option
  1418. * with the value UseMulticast, the client records the receipt of the
  1419. * message and sends subsequent messages to the server through the
  1420. * interface on which the message was received using multicast. The
  1421. * client resends the original message using multicast.
  1422. *
  1423. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1424. * | OPTION_IA_NA | option-len |
  1425. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1426. * | IAID (4 octets) |
  1427. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1428. * | T1 |
  1429. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1430. * | T2 |
  1431. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1432. * | |
  1433. * . IA_NA-options .
  1434. * . .
  1435. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1436. *
  1437. *
  1438. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1439. * | OPTION_IAADDR | option-len |
  1440. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1441. * | |
  1442. * | IPv6 address |
  1443. * | |
  1444. * | |
  1445. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1446. * | preferred-lifetime |
  1447. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1448. * | valid-lifetime |
  1449. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1450. * . .
  1451. * . IAaddr-options .
  1452. * . .
  1453. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1454. */
  1455. free(client6_data.ia_na);
  1456. client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA);
  1457. if (!client6_data.ia_na) {
  1458. bb_error_msg("no %s option, ignoring packet", "IA_NA");
  1459. continue;
  1460. }
  1461. if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) {
  1462. bb_error_msg("IA_NA option is too short:%d bytes", client6_data.ia_na->len);
  1463. continue;
  1464. }
  1465. iaaddr = d6_find_option(client6_data.ia_na->data + 4 + 4 + 4,
  1466. client6_data.ia_na->data + client6_data.ia_na->len,
  1467. D6_OPT_IAADDR
  1468. );
  1469. if (!iaaddr) {
  1470. bb_error_msg("no %s option, ignoring packet", "IAADDR");
  1471. continue;
  1472. }
  1473. if (iaaddr->len < (16 + 4 + 4)) {
  1474. bb_error_msg("IAADDR option is too short:%d bytes", iaaddr->len);
  1475. continue;
  1476. }
  1477. /* Note: the address is sufficiently aligned for cast:
  1478. * we _copied_ IA-NA, and copy is always well-aligned.
  1479. */
  1480. requested_ipv6 = (struct in6_addr*) iaaddr->data;
  1481. move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4);
  1482. lease_seconds = ntohl(lease_seconds);
  1483. /* paranoia: must not be too small and not prone to overflows */
  1484. if (lease_seconds < 0x10)
  1485. lease_seconds = 0x10;
  1486. /// TODO: check for 0 lease time?
  1487. if (lease_seconds > 0x7fffffff / 1000)
  1488. lease_seconds = 0x7fffffff / 1000;
  1489. /* enter bound state */
  1490. timeout = lease_seconds / 2;
  1491. bb_error_msg("lease obtained, lease time %u",
  1492. /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
  1493. d6_run_script(&packet, state == REQUESTING ? "bound" : "renew");
  1494. state = BOUND;
  1495. change_listen_mode(LISTEN_NONE);
  1496. if (opt & OPT_q) { /* quit after lease */
  1497. goto ret0;
  1498. }
  1499. /* future renew failures should not exit (JM) */
  1500. opt &= ~OPT_n;
  1501. #if BB_MMU /* NOMMU case backgrounded earlier */
  1502. if (!(opt & OPT_f)) {
  1503. client_background();
  1504. /* do not background again! */
  1505. opt = ((opt & ~OPT_b) | OPT_f);
  1506. }
  1507. #endif
  1508. already_waited_sec = 0;
  1509. continue; /* back to main loop */
  1510. }
  1511. continue;
  1512. /* case BOUND: - ignore all packets */
  1513. /* case RELEASED: - ignore all packets */
  1514. }
  1515. /* back to main loop */
  1516. } /* for (;;) - main loop ends */
  1517. ret0:
  1518. if (opt & OPT_R) /* release on quit */
  1519. perform_d6_release(&srv6_buf, requested_ipv6);
  1520. retval = 0;
  1521. ret:
  1522. /*if (client_config.pidfile) - remove_pidfile has its own check */
  1523. remove_pidfile(client_config.pidfile);
  1524. return retval;
  1525. }