d6_dhcpc.c 59 KB

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