blob: 76b087b921f2d86baac769ac2d42d23a18c7cae2 [file] [log] [blame]
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001/* vi: set sw=4 ts=4: */
2/*
3 * DHCPv6 client.
4 *
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +02005 * Copyright (C) 2011-2017 Denys Vlasenko.
Denys Vlasenko9ba75042011-11-07 15:55:39 +01006 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01009//config:config UDHCPC6
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "udhcpc6 (21 kb)"
Denys Vlasenkocc45cbc2019-04-13 17:32:40 +020011//config: default y
Mike Frysinger3da46c82012-05-02 21:45:35 -040012//config: depends on FEATURE_IPV6
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: udhcpc6 is a DHCPv6 client
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020015//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//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".
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020024//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//config: You can request FQDN to be given by server using "-O fqdn".
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020031//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: You can request POSIX timezone with "-O tz" and timezone name
38//config: with "-O timezone".
Samuel Mendoza-Jonas23cbd7d2018-05-14 14:29:12 +100039//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".
Denys Vlasenko9ba75042011-11-07 15:55:39 +010047
48//applet:IF_UDHCPC6(APPLET(udhcpc6, BB_DIR_USR_BIN, BB_SUID_DROP))
49
Denys Vlasenko8cab6672012-04-20 14:48:00 +020050//kbuild:lib-$(CONFIG_UDHCPC6) += d6_dhcpc.o d6_packet.o d6_socket.o common.o socket.o signalpipe.o
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020051//kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC3646) += domain_codec.o
52//kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC4704) += domain_codec.o
Denys Vlasenko9ba75042011-11-07 15:55:39 +010053
54#include <syslog.h>
55/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
56#define WANT_PIDFILE 1
57#include "common.h"
58#include "dhcpd.h"
59#include "dhcpc.h"
60#include "d6_common.h"
61
62#include <netinet/if_ether.h>
63#include <netpacket/packet.h>
64#include <linux/filter.h>
65
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +020066/* "struct client_data_t client_data" is in bb_common_bufsiz1 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +010067
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020068static const struct dhcp_optflag d6_optflags[] = {
69#if ENABLE_FEATURE_UDHCPC6_RFC3646
70 { OPTION_6RD | OPTION_LIST | OPTION_REQ, D6_OPT_DNS_SERVERS },
71 { OPTION_DNS_STRING | OPTION_LIST | OPTION_REQ, D6_OPT_DOMAIN_LIST },
72#endif
73#if ENABLE_FEATURE_UDHCPC6_RFC4704
74 { OPTION_DNS_STRING, D6_OPT_CLIENT_FQDN },
75#endif
76#if ENABLE_FEATURE_UDHCPC6_RFC4833
77 { OPTION_STRING, D6_OPT_TZ_POSIX },
78 { OPTION_STRING, D6_OPT_TZ_NAME },
79#endif
Samuel Mendoza-Jonas23cbd7d2018-05-14 14:29:12 +100080#if ENABLE_FEATURE_UDHCPC6_RFC5970
81 { OPTION_STRING, D6_OPT_BOOT_URL },
82 { OPTION_STRING, D6_OPT_BOOT_PARAM },
83#endif
Samuel Mendoza-Jonas30f4d522018-05-14 14:29:13 +100084 { OPTION_STRING, 0xd1 }, /* DHCP_PXE_CONF_FILE */
85 { OPTION_STRING, 0xd2 }, /* DHCP_PXE_PATH_PREFIX */
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +020086 { 0, 0 }
87};
88/* Must match d6_optflags[] order */
89static const char d6_option_strings[] ALIGN1 =
90#if ENABLE_FEATURE_UDHCPC6_RFC3646
91 "dns" "\0" /* D6_OPT_DNS_SERVERS */
92 "search" "\0" /* D6_OPT_DOMAIN_LIST */
93#endif
94#if ENABLE_FEATURE_UDHCPC6_RFC4704
95 "fqdn" "\0" /* D6_OPT_CLIENT_FQDN */
96#endif
97#if ENABLE_FEATURE_UDHCPC6_RFC4833
98 "tz" "\0" /* D6_OPT_TZ_POSIX */
99 "timezone" "\0" /* D6_OPT_TZ_NAME */
100#endif
Samuel Mendoza-Jonas23cbd7d2018-05-14 14:29:12 +1000101#if ENABLE_FEATURE_UDHCPC6_RFC5970
102 "bootfile_url" "\0" /* D6_OPT_BOOT_URL */
103 "bootfile_param" "\0" /* D6_OPT_BOOT_PARAM */
104#endif
Samuel Mendoza-Jonas30f4d522018-05-14 14:29:13 +1000105 "pxeconffile" "\0" /* DHCP_PXE_CONF_FILE */
106 "pxepathprefix" "\0" /* DHCP_PXE_PATH_PREFIX */
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200107 "\0";
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100108
109#if ENABLE_LONG_OPTS
Denys Vlasenko7e21f042011-11-08 11:39:41 +0100110static const char udhcpc6_longopts[] ALIGN1 =
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100111 "interface\0" Required_argument "i"
112 "now\0" No_argument "n"
113 "pidfile\0" Required_argument "p"
114 "quit\0" No_argument "q"
115 "release\0" No_argument "R"
116 "request\0" Required_argument "r"
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100117 "requestprefix\0" No_argument "d"
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100118 "script\0" Required_argument "s"
119 "timeout\0" Required_argument "T"
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100120 "retries\0" Required_argument "t"
121 "tryagain\0" Required_argument "A"
122 "syslog\0" No_argument "S"
123 "request-option\0" Required_argument "O"
124 "no-default-options\0" No_argument "o"
125 "foreground\0" No_argument "f"
Eivind Versvik22a63392019-08-24 17:23:48 +0200126 "stateless\0" No_argument "l"
Denys Vlasenkoed820cc2017-05-08 15:11:02 +0200127 USE_FOR_MMU(
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100128 "background\0" No_argument "b"
Denys Vlasenkoed820cc2017-05-08 15:11:02 +0200129 )
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100130/// IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a")
131 IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P")
132 ;
133#endif
134/* Must match getopt32 option string order */
135enum {
136 OPT_i = 1 << 0,
137 OPT_n = 1 << 1,
138 OPT_p = 1 << 2,
139 OPT_q = 1 << 3,
140 OPT_R = 1 << 4,
141 OPT_r = 1 << 5,
142 OPT_s = 1 << 6,
143 OPT_T = 1 << 7,
144 OPT_t = 1 << 8,
145 OPT_S = 1 << 9,
146 OPT_A = 1 << 10,
147 OPT_O = 1 << 11,
148 OPT_o = 1 << 12,
149 OPT_x = 1 << 13,
150 OPT_f = 1 << 14,
Eivind Versvik22a63392019-08-24 17:23:48 +0200151 OPT_l = 1 << 15,
152 OPT_d = 1 << 16,
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100153/* The rest has variable bit positions, need to be clever */
Eivind Versvik22a63392019-08-24 17:23:48 +0200154 OPTBIT_d = 16,
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100155 USE_FOR_MMU( OPTBIT_b,)
156 ///IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
157 IF_FEATURE_UDHCP_PORT( OPTBIT_P,)
158 USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,)
159 ///IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,)
160 IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,)
161};
162
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200163#if ENABLE_FEATURE_UDHCPC6_RFC4704
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200164static const char opt_fqdn_req[] = {
165 (D6_OPT_CLIENT_FQDN >> 8), (D6_OPT_CLIENT_FQDN & 0xff),
Denys Vlasenko470bebe2017-06-27 18:31:08 +0200166 0, 2, /* optlen */
167 0, /* flags: */
168 /* S=0: server SHOULD NOT perform AAAA RR updates */
169 /* O=0: client MUST set this bit to 0 */
170 /* N=0: server SHOULD perform updates (PTR RR only in our case, since S=0) */
171 0 /* empty DNS-encoded name */
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200172};
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200173#endif
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100174
175/*** Utility functions ***/
176
177static void *d6_find_option(uint8_t *option, uint8_t *option_end, unsigned code)
178{
179 /* "length minus 4" */
180 int len_m4 = option_end - option - 4;
181 while (len_m4 >= 0) {
182 /* Next option's len is too big? */
Denys Vlasenko68c5b282011-11-07 16:21:24 +0100183 if (option[3] > len_m4)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100184 return NULL; /* yes. bogus packet! */
185 /* So far we treat any opts with code >255
186 * or len >255 as bogus, and stop at once.
187 * This simplifies big-endian handling.
188 */
Denys Vlasenko68c5b282011-11-07 16:21:24 +0100189 if (option[0] != 0 || option[2] != 0)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100190 return NULL;
191 /* Option seems to be valid */
192 /* Does its code match? */
Denys Vlasenko68c5b282011-11-07 16:21:24 +0100193 if (option[1] == code)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100194 return option; /* yes! */
Denys Vlasenko68c5b282011-11-07 16:21:24 +0100195 len_m4 -= option[3] + 4;
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200196 option += option[3] + 4;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100197 }
198 return NULL;
199}
200
201static void *d6_copy_option(uint8_t *option, uint8_t *option_end, unsigned code)
202{
203 uint8_t *opt = d6_find_option(option, option_end, code);
204 if (!opt)
205 return opt;
Ron Yorstond840c5d2015-07-19 23:05:20 +0200206 return xmemdup(opt, opt[3] + 4);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100207}
208
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100209/*** Script execution code ***/
210
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100211static char** new_env(void)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100212{
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100213 client6_data.env_ptr = xrealloc_vector(client6_data.env_ptr, 3, client6_data.env_idx);
214 return &client6_data.env_ptr[client6_data.env_idx++];
215}
216
David Decotigny8f48fc02018-05-24 08:30:15 -0700217static char *string_option_to_env(const uint8_t *option,
218 const uint8_t *option_end)
Samuel Mendoza-Jonasbcdec1a2018-05-14 14:29:11 +1000219{
220 const char *ptr, *name = NULL;
221 unsigned val_len;
222 int i;
223
224 ptr = d6_option_strings;
225 i = 0;
226 while (*ptr) {
227 if (d6_optflags[i].code == option[1]) {
228 name = ptr;
229 goto found;
230 }
231 ptr += strlen(ptr) + 1;
232 i++;
233 }
234 bb_error_msg("can't find option name for 0x%x, skipping", option[1]);
235 return NULL;
236
237 found:
238 val_len = (option[2] << 8) | option[3];
239 if (val_len + &option[D6_OPT_DATA] > option_end) {
James Byrne69374872019-07-02 11:35:03 +0200240 bb_simple_error_msg("option data exceeds option length");
Samuel Mendoza-Jonasbcdec1a2018-05-14 14:29:11 +1000241 return NULL;
242 }
243 return xasprintf("%s=%.*s", name, val_len, (char*)option + 4);
244}
245
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100246/* put all the parameters into the environment */
David Decotigny8f48fc02018-05-24 08:30:15 -0700247static void option_to_env(const uint8_t *option, const uint8_t *option_end)
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100248{
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200249#if ENABLE_FEATURE_UDHCPC6_RFC3646
250 int addrs, option_offset;
251#endif
Denys Vlasenkoab030612017-03-27 22:49:12 +0200252 /* "length minus 4" */
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100253 int len_m4 = option_end - option - 4;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200254
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100255 while (len_m4 >= 0) {
256 uint32_t v32;
257 char ipv6str[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")];
258
259 if (option[0] != 0 || option[2] != 0)
260 break;
261
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200262 /* Check if option-length exceeds size of option */
263 if (option[3] > len_m4)
264 break;
265
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100266 switch (option[1]) {
267 //case D6_OPT_CLIENTID:
268 //case D6_OPT_SERVERID:
269 case D6_OPT_IA_NA:
270 case D6_OPT_IA_PD:
271 option_to_env(option + 16, option + 4 + option[3]);
272 break;
273 //case D6_OPT_IA_TA:
274 case D6_OPT_IAADDR:
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200275/* 0 1 2 3
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100276 * 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
277 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278 * | OPTION_IAADDR | option-len |
279 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
280 * | |
281 * | IPv6 address |
282 * | |
283 * | |
284 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
285 * | preferred-lifetime |
286 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
287 * | valid-lifetime |
288 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
289 */
David Decotignya174c792018-05-24 08:30:16 -0700290 /* Make sure payload contains an address */
291 if (option[3] < 24)
292 break;
293
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100294 sprint_nip6(ipv6str, option + 4);
295 *new_env() = xasprintf("ipv6=%s", ipv6str);
296
297 move_from_unaligned32(v32, option + 4 + 16 + 4);
298 *new_env() = xasprintf("lease=%u", (unsigned)v32);
299 break;
300
301 //case D6_OPT_ORO:
302 //case D6_OPT_PREFERENCE:
303 //case D6_OPT_ELAPSED_TIME:
304 //case D6_OPT_RELAY_MSG:
305 //case D6_OPT_AUTH:
306 //case D6_OPT_UNICAST:
307 //case D6_OPT_STATUS_CODE:
308 //case D6_OPT_RAPID_COMMIT:
309 //case D6_OPT_USER_CLASS:
310 //case D6_OPT_VENDOR_CLASS:
311 //case D6_OPT_VENDOR_OPTS:
312 //case D6_OPT_INTERFACE_ID:
313 //case D6_OPT_RECONF_MSG:
314 //case D6_OPT_RECONF_ACCEPT:
315
316 case D6_OPT_IAPREFIX:
317/* 0 1 2 3
318 * 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
319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320 * | OPTION_IAPREFIX | option-length |
321 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
322 * | preferred-lifetime |
323 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
324 * | valid-lifetime |
325 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
326 * | prefix-length | |
327 * +-+-+-+-+-+-+-+-+ IPv6 prefix |
328 * | (16 octets) |
329 * | |
330 * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331 * | |
332 * +-+-+-+-+-+-+-+-+
333 */
Denys Vlasenko688cb3b2018-01-16 16:00:13 +0100334 move_from_unaligned32(v32, option + 4 + 4);
335 *new_env() = xasprintf("ipv6prefix_lease=%u", (unsigned)v32);
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100336
Denys Vlasenko688cb3b2018-01-16 16:00:13 +0100337 sprint_nip6(ipv6str, option + 4 + 4 + 4 + 1);
338 *new_env() = xasprintf("ipv6prefix=%s/%u", ipv6str, (unsigned)(option[4 + 4 + 4]));
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200339 break;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200340#if ENABLE_FEATURE_UDHCPC6_RFC3646
341 case D6_OPT_DNS_SERVERS: {
342 char *dlist;
343
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200344 /* Make sure payload-size is a multiple of 16 */
345 if ((option[3] & 0x0f) != 0)
346 break;
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200347
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200348 /* Get the number of addresses on the option */
349 addrs = option[3] >> 4;
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200350
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200351 /* Setup environment variable */
352 *new_env() = dlist = xmalloc(4 + addrs * 40 - 1);
353 dlist = stpcpy(dlist, "dns=");
354 option_offset = 0;
355
356 while (addrs--) {
357 sprint_nip6(dlist, option + 4 + option_offset);
358 dlist += 39;
359 option_offset += 16;
360 if (addrs)
361 *dlist++ = ' ';
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200362 }
363
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200364 break;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200365 }
366 case D6_OPT_DOMAIN_LIST: {
367 char *dlist;
368
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200369 dlist = dname_dec(option + 4, (option[2] << 8) | option[3], "search=");
370 if (!dlist)
371 break;
372 *new_env() = dlist;
373 break;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200374 }
375#endif
376#if ENABLE_FEATURE_UDHCPC6_RFC4704
377 case D6_OPT_CLIENT_FQDN: {
378 char *dlist;
379
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200380 if (option[3] == 0)
381 break;
382 /* Work around broken ISC DHCPD6.
383 * ISC DHCPD6 does not implement RFC 4704 correctly: It says the first
384 * byte of option-payload should contain flags where the bits 7-3 are
385 * reserved for future use and MUST be zero. Instead ISC DHCPD6 just
386 * writes the entire FQDN as string to option-payload. We assume a
387 * broken server here if any of the reserved bits are set.
388 */
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200389 if (option[4] & 0xf8) {
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200390 *new_env() = xasprintf("fqdn=%.*s", (int)option[3], (char*)option + 4);
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200391 break;
392 }
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200393 dlist = dname_dec(option + 5, (/*(option[2] << 8) |*/ option[3]) - 1, "fqdn=");
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200394 if (!dlist)
395 break;
396 *new_env() = dlist;
397 break;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200398 }
399#endif
400#if ENABLE_FEATURE_UDHCPC6_RFC4833
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200401 /* RFC 4833 Timezones */
402 case D6_OPT_TZ_POSIX:
403 *new_env() = xasprintf("tz=%.*s", (int)option[3], (char*)option + 4);
404 break;
405 case D6_OPT_TZ_NAME:
406 *new_env() = xasprintf("tz_name=%.*s", (int)option[3], (char*)option + 4);
407 break;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200408#endif
Samuel Mendoza-Jonas23cbd7d2018-05-14 14:29:12 +1000409 case D6_OPT_BOOT_URL:
410 case D6_OPT_BOOT_PARAM:
Samuel Mendoza-Jonas30f4d522018-05-14 14:29:13 +1000411 case 0xd1: /* DHCP_PXE_CONF_FILE */
412 case 0xd2: /* DHCP_PXE_PATH_PREFIX */
Samuel Mendoza-Jonas23cbd7d2018-05-14 14:29:12 +1000413 {
414 char *tmp = string_option_to_env(option, option_end);
415 if (tmp)
416 *new_env() = tmp;
417 break;
418 }
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100419 }
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100420 len_m4 -= 4 + option[3];
Denys Vlasenko64d58aa2017-03-27 22:22:09 +0200421 option += 4 + option[3];
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100422 }
423}
424
David Decotigny8f48fc02018-05-24 08:30:15 -0700425static char **fill_envp(const uint8_t *option, const uint8_t *option_end)
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100426{
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100427 char **envp, **curr;
428
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100429 client6_data.env_ptr = NULL;
430 client6_data.env_idx = 0;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100431
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200432 *new_env() = xasprintf("interface=%s", client_data.interface);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100433
David Decotigny8f48fc02018-05-24 08:30:15 -0700434 if (option)
435 option_to_env(option, option_end);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100436
Denys Vlasenkoa092a892011-11-16 20:17:12 +0100437 envp = curr = client6_data.env_ptr;
438 while (*curr)
439 putenv(*curr++);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100440
441 return envp;
442}
443
444/* Call a script with a par file and env vars */
David Decotigny8f48fc02018-05-24 08:30:15 -0700445static void d6_run_script(const uint8_t *option, const uint8_t *option_end,
446 const char *name)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100447{
448 char **envp, **curr;
449 char *argv[3];
450
David Decotigny8f48fc02018-05-24 08:30:15 -0700451 envp = fill_envp(option, option_end);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100452
453 /* call script */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200454 log1("executing %s %s", client_data.script, name);
455 argv[0] = (char*) client_data.script;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100456 argv[1] = (char*) name;
457 argv[2] = NULL;
458 spawn_and_wait(argv);
459
460 for (curr = envp; *curr; curr++) {
461 log2(" %s", *curr);
462 bb_unsetenv_and_free(*curr);
463 }
464 free(envp);
465}
466
David Decotigny8f48fc02018-05-24 08:30:15 -0700467/* Call a script with a par file and no env var */
468static void d6_run_script_no_option(const char *name)
469{
470 d6_run_script(NULL, NULL, name);
471}
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100472
473/*** Sending/receiving packets ***/
474
475static ALWAYS_INLINE uint32_t random_xid(void)
476{
477 uint32_t t = rand() & htonl(0x00ffffff);
478 return t;
479}
480
481/* Initialize the packet with the proper defaults */
482static uint8_t *init_d6_packet(struct d6_packet *packet, char type, uint32_t xid)
483{
Denys Vlasenko11e024a2019-09-24 14:01:00 +0200484 uint8_t *ptr;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100485 struct d6_option *clientid;
Denys Vlasenko11e024a2019-09-24 14:01:00 +0200486 unsigned secs;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100487
488 memset(packet, 0, sizeof(*packet));
489
490 packet->d6_xid32 = xid;
491 packet->d6_msg_type = type;
492
Denys Vlasenko11e024a2019-09-24 14:01:00 +0200493 /* ELAPSED_TIME option is required to be present by the RFC,
494 * and some servers do check for its presense. [which?]
495 */
496 ptr = packet->d6_options; /* NB: it is 32-bit aligned */
497 *((uint32_t*)ptr) = htonl((D6_OPT_ELAPSED_TIME << 16) + 2);
498 ptr += 4;
499 client_data.last_secs = monotonic_sec();
500 if (client_data.first_secs == 0)
501 client_data.first_secs = client_data.last_secs;
502 secs = client_data.last_secs - client_data.first_secs;
503 *((uint16_t*)ptr) = (secs < 0xffff) ? htons(secs) : 0xffff;
504 ptr += 2;
505
506 /* add CLIENTID option */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200507 clientid = (void*)client_data.clientid;
Denys Vlasenko11e024a2019-09-24 14:01:00 +0200508 return mempcpy(ptr, clientid, clientid->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100509}
510
511static uint8_t *add_d6_client_options(uint8_t *ptr)
512{
Denys Vlasenko60275972018-05-14 11:06:35 +0200513 struct option_set *curr;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200514 uint8_t *start = ptr;
515 unsigned option;
Denys Vlasenko60275972018-05-14 11:06:35 +0200516 uint16_t len;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100517
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200518 ptr += 4;
519 for (option = 1; option < 256; option++) {
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200520 if (client_data.opt_mask[option >> 3] & (1 << (option & 7))) {
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200521 ptr[0] = (option >> 8);
522 ptr[1] = option;
523 ptr += 2;
524 }
525 }
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100526
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200527 if ((ptr - start - 4) != 0) {
528 start[0] = (D6_OPT_ORO >> 8);
529 start[1] = D6_OPT_ORO;
530 start[2] = ((ptr - start - 4) >> 8);
531 start[3] = (ptr - start - 4);
532 } else
533 ptr = start;
534
535#if ENABLE_FEATURE_UDHCPC6_RFC4704
536 ptr = mempcpy(ptr, &opt_fqdn_req, sizeof(opt_fqdn_req));
537#endif
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100538 /* Add -x options if any */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200539 curr = client_data.options;
Denys Vlasenko60275972018-05-14 11:06:35 +0200540 while (curr) {
541 len = (curr->data[D6_OPT_LEN] << 8) | curr->data[D6_OPT_LEN + 1];
542 ptr = mempcpy(ptr, curr->data, D6_OPT_DATA + len);
543 curr = curr->next;
544 }
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200545
546 return ptr;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100547}
548
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200549static int d6_mcast_from_client_data_ifindex(struct d6_packet *packet, uint8_t *end)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100550{
Denys Vlasenko0d75e8b2019-04-13 19:43:15 +0200551 /* FF02::1:2 is "All_DHCP_Relay_Agents_and_Servers" address */
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100552 static const uint8_t FF02__1_2[16] = {
553 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
554 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,
555 };
Uwe Glaeserfaab9062020-06-21 02:56:12 +0200556 /* IPv6 requires different multicast contents in Ethernet Frame (RFC 2464) */
557 static const uint8_t MAC_DHCP6MCAST_ADDR[6] ALIGN2 = {
558 0x33, 0x33, 0x00, 0x01, 0x00, 0x02,
559 };
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100560
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100561 return d6_send_raw_packet_from_client_data_ifindex(
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100562 packet, (end - (uint8_t*) packet),
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +0200563 /*src*/ &client6_data.ll_ip6, CLIENT_PORT6,
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100564 /*dst*/ (struct in6_addr*)FF02__1_2, SERVER_PORT6, MAC_DHCP6MCAST_ADDR
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100565 );
566}
567
Eivind Versvik22a63392019-08-24 17:23:48 +0200568/* RFC 3315 18.1.5. Creation and Transmission of Information-request Messages
569 *
570 * The client uses an Information-request message to obtain
571 * configuration information without having addresses assigned to it.
572 *
573 * The client sets the "msg-type" field to INFORMATION-REQUEST. The
574 * client generates a transaction ID and inserts this value in the
575 * "transaction-id" field.
576 *
577 * The client SHOULD include a Client Identifier option to identify
578 * itself to the server. If the client does not include a Client
579 * Identifier option, the server will not be able to return any client-
580 * specific options to the client, or the server may choose not to
581 * respond to the message at all. The client MUST include a Client
582 * Identifier option if the Information-Request message will be
583 * authenticated.
584 *
585 * The client MUST include an Option Request option (see section 22.7)
586 * to indicate the options the client is interested in receiving. The
587 * client MAY include options with data values as hints to the server
588 * about parameter values the client would like to have returned.
589 */
590/* NOINLINE: limit stack usage in caller */
591static NOINLINE int send_d6_info_request(uint32_t xid)
592{
593 struct d6_packet packet;
594 uint8_t *opt_ptr;
595
596 /* Fill in: msg type, client id */
597 opt_ptr = init_d6_packet(&packet, D6_MSG_INFORMATION_REQUEST, xid);
598
599 /* Add options:
600 * "param req" option according to -O, options specified with -x
601 */
602 opt_ptr = add_d6_client_options(opt_ptr);
603
604 bb_error_msg("sending %s", "info request");
605 return d6_mcast_from_client_data_ifindex(&packet, opt_ptr);
606}
607
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100608/* Milticast a DHCPv6 Solicit packet to the network, with an optionally requested IP.
609 *
610 * RFC 3315 17.1.1. Creation of Solicit Messages
611 *
612 * The client MUST include a Client Identifier option to identify itself
613 * to the server. The client includes IA options for any IAs to which
614 * it wants the server to assign addresses. The client MAY include
615 * addresses in the IAs as a hint to the server about addresses for
616 * which the client has a preference. ...
617 *
618 * The client uses IA_NA options to request the assignment of non-
619 * temporary addresses and uses IA_TA options to request the assignment
620 * of temporary addresses. Either IA_NA or IA_TA options, or a
621 * combination of both, can be included in DHCP messages.
622 *
623 * The client SHOULD include an Option Request option (see section 22.7)
624 * to indicate the options the client is interested in receiving. The
625 * client MAY additionally include instances of those options that are
626 * identified in the Option Request option, with data values as hints to
627 * the server about parameter values the client would like to have
628 * returned.
629 *
630 * The client includes a Reconfigure Accept option (see section 22.20)
631 * if the client is willing to accept Reconfigure messages from the
632 * server.
633 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 | OPTION_CLIENTID | option-len |
635 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 . .
637 . DUID .
638 . (variable length) .
639 . .
640 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
641
642
643 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644 | OPTION_IA_NA | option-len |
645 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646 | IAID (4 octets) |
647 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 | T1 |
649 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 | T2 |
651 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 | |
653 . IA_NA-options .
654 . .
655 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656
657
658 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 | OPTION_IAADDR | option-len |
660 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661 | |
662 | IPv6 address |
663 | |
664 | |
665 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
666 | preferred-lifetime |
667 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
668 | valid-lifetime |
669 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670 . .
671 . IAaddr-options .
672 . .
673 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
674
675
676 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 | OPTION_ORO | option-len |
678 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 | requested-option-code-1 | requested-option-code-2 |
680 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 | ... |
682 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
683
684
685 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 | OPTION_RECONF_ACCEPT | 0 |
687 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 */
689/* NOINLINE: limit stack usage in caller */
690static NOINLINE int send_d6_discover(uint32_t xid, struct in6_addr *requested_ipv6)
691{
692 struct d6_packet packet;
693 uint8_t *opt_ptr;
694 unsigned len;
695
696 /* Fill in: msg type, client id */
697 opt_ptr = init_d6_packet(&packet, D6_MSG_SOLICIT, xid);
698
699 /* Create new IA_NA, optionally with included IAADDR with requested IP */
700 free(client6_data.ia_na);
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100701 client6_data.ia_na = NULL;
702 if (option_mask32 & OPT_r) {
703 len = requested_ipv6 ? 2+2+4+4+4 + 2+2+16+4+4 : 2+2+4+4+4;
704 client6_data.ia_na = xzalloc(len);
705 client6_data.ia_na->code = D6_OPT_IA_NA;
706 client6_data.ia_na->len = len - 4;
Denys Vlasenko72f12ac2019-06-11 18:18:48 +0200707 *(bb__aliased_uint32_t*)client6_data.ia_na->data = rand(); /* IAID */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100708 if (requested_ipv6) {
709 struct d6_option *iaaddr = (void*)(client6_data.ia_na->data + 4+4+4);
710 iaaddr->code = D6_OPT_IAADDR;
711 iaaddr->len = 16+4+4;
712 memcpy(iaaddr->data, requested_ipv6, 16);
713 }
714 opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, len);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100715 }
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100716
717 /* IA_PD */
718 free(client6_data.ia_pd);
719 client6_data.ia_pd = NULL;
720 if (option_mask32 & OPT_d) {
721 len = 2+2+4+4+4;
722 client6_data.ia_pd = xzalloc(len);
723 client6_data.ia_pd->code = D6_OPT_IA_PD;
724 client6_data.ia_pd->len = len - 4;
Denys Vlasenko72f12ac2019-06-11 18:18:48 +0200725 *(bb__aliased_uint32_t*)client6_data.ia_pd->data = rand(); /* IAID */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100726 opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, len);
727 }
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100728
729 /* Add options:
730 * "param req" option according to -O, options specified with -x
731 */
732 opt_ptr = add_d6_client_options(opt_ptr);
733
James Byrne253c4e72019-04-12 17:01:51 +0000734 bb_info_msg("sending %s", "discover");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200735 return d6_mcast_from_client_data_ifindex(&packet, opt_ptr);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100736}
737
738/* Multicast a DHCPv6 request message
739 *
740 * RFC 3315 18.1.1. Creation and Transmission of Request Messages
741 *
742 * The client uses a Request message to populate IAs with addresses and
743 * obtain other configuration information. The client includes one or
744 * more IA options in the Request message. The server then returns
745 * addresses and other information about the IAs to the client in IA
746 * options in a Reply message.
747 *
748 * The client generates a transaction ID and inserts this value in the
749 * "transaction-id" field.
750 *
751 * The client places the identifier of the destination server in a
752 * Server Identifier option.
753 *
754 * The client MUST include a Client Identifier option to identify itself
755 * to the server. The client adds any other appropriate options,
756 * including one or more IA options (if the client is requesting that
757 * the server assign it some network addresses).
758 *
759 * The client MUST include an Option Request option (see section 22.7)
760 * to indicate the options the client is interested in receiving. The
761 * client MAY include options with data values as hints to the server
762 * about parameter values the client would like to have returned.
763 *
764 * The client includes a Reconfigure Accept option (see section 22.20)
765 * indicating whether or not the client is willing to accept Reconfigure
766 * messages from the server.
767 */
768/* NOINLINE: limit stack usage in caller */
769static NOINLINE int send_d6_select(uint32_t xid)
770{
771 struct d6_packet packet;
772 uint8_t *opt_ptr;
773
774 /* Fill in: msg type, client id */
775 opt_ptr = init_d6_packet(&packet, D6_MSG_REQUEST, xid);
776
777 /* server id */
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200778 opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100779 /* IA NA (contains requested IP) */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100780 if (client6_data.ia_na)
781 opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
782 /* IA PD */
783 if (client6_data.ia_pd)
784 opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100785
786 /* Add options:
787 * "param req" option according to -O, options specified with -x
788 */
789 opt_ptr = add_d6_client_options(opt_ptr);
790
James Byrne253c4e72019-04-12 17:01:51 +0000791 bb_info_msg("sending %s", "select");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200792 return d6_mcast_from_client_data_ifindex(&packet, opt_ptr);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100793}
794
795/* Unicast or broadcast a DHCP renew message
796 *
797 * RFC 3315 18.1.3. Creation and Transmission of Renew Messages
798 *
799 * To extend the valid and preferred lifetimes for the addresses
800 * associated with an IA, the client sends a Renew message to the server
801 * from which the client obtained the addresses in the IA containing an
802 * IA option for the IA. The client includes IA Address options in the
803 * IA option for the addresses associated with the IA. The server
804 * determines new lifetimes for the addresses in the IA according to the
805 * administrative configuration of the server. The server may also add
806 * new addresses to the IA. The server may remove addresses from the IA
807 * by setting the preferred and valid lifetimes of those addresses to
808 * zero.
809 *
810 * The server controls the time at which the client contacts the server
811 * to extend the lifetimes on assigned addresses through the T1 and T2
812 * parameters assigned to an IA.
813 *
814 * At time T1 for an IA, the client initiates a Renew/Reply message
815 * exchange to extend the lifetimes on any addresses in the IA. The
816 * client includes an IA option with all addresses currently assigned to
817 * the IA in its Renew message.
818 *
819 * If T1 or T2 is set to 0 by the server (for an IA_NA) or there are no
820 * T1 or T2 times (for an IA_TA), the client may send a Renew or Rebind
821 * message, respectively, at the client's discretion.
822 *
823 * The client sets the "msg-type" field to RENEW. The client generates
824 * a transaction ID and inserts this value in the "transaction-id"
825 * field.
826 *
827 * The client places the identifier of the destination server in a
828 * Server Identifier option.
829 *
830 * The client MUST include a Client Identifier option to identify itself
831 * to the server. The client adds any appropriate options, including
832 * one or more IA options. The client MUST include the list of
833 * addresses the client currently has associated with the IAs in the
834 * Renew message.
835 *
836 * The client MUST include an Option Request option (see section 22.7)
837 * to indicate the options the client is interested in receiving. The
838 * client MAY include options with data values as hints to the server
839 * about parameter values the client would like to have returned.
840 */
841/* NOINLINE: limit stack usage in caller */
842static NOINLINE int send_d6_renew(uint32_t xid, struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
843{
844 struct d6_packet packet;
845 uint8_t *opt_ptr;
846
847 /* Fill in: msg type, client id */
848 opt_ptr = init_d6_packet(&packet, DHCPREQUEST, xid);
849
850 /* server id */
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200851 opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100852 /* IA NA (contains requested IP) */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100853 if (client6_data.ia_na)
854 opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
855 /* IA PD */
856 if (client6_data.ia_pd)
857 opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100858
859 /* Add options:
860 * "param req" option according to -O, options specified with -x
861 */
862 opt_ptr = add_d6_client_options(opt_ptr);
863
James Byrne253c4e72019-04-12 17:01:51 +0000864 bb_info_msg("sending %s", "renew");
Denys Vlasenko148788e2018-06-21 17:36:22 +0200865 if (server_ipv6)
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100866 return d6_send_kernel_packet_from_client_data_ifindex(
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100867 &packet, (opt_ptr - (uint8_t*) &packet),
Denys Vlasenko04ac6e02013-01-28 15:25:35 +0100868 our_cur_ipv6, CLIENT_PORT6,
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100869 server_ipv6, SERVER_PORT6
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100870 );
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200871 return d6_mcast_from_client_data_ifindex(&packet, opt_ptr);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100872}
873
874/* Unicast a DHCP release message */
Denys Vlasenko9d05ad02018-11-03 23:34:03 +0100875static
876ALWAYS_INLINE /* one caller, help compiler to use this fact */
877int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100878{
879 struct d6_packet packet;
880 uint8_t *opt_ptr;
881
882 /* Fill in: msg type, client id */
883 opt_ptr = init_d6_packet(&packet, D6_MSG_RELEASE, random_xid());
884 /* server id */
Denys Vlasenko234b82c2017-06-26 19:42:48 +0200885 opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100886 /* IA NA (contains our current IP) */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +0100887 if (client6_data.ia_na)
888 opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2);
889 /* IA PD */
890 if (client6_data.ia_pd)
891 opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2);
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100892
James Byrne253c4e72019-04-12 17:01:51 +0000893 bb_info_msg("sending %s", "release");
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100894 return d6_send_kernel_packet_from_client_data_ifindex(
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100895 &packet, (opt_ptr - (uint8_t*) &packet),
Denys Vlasenko04ac6e02013-01-28 15:25:35 +0100896 our_cur_ipv6, CLIENT_PORT6,
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100897 server_ipv6, SERVER_PORT6
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100898 );
899}
900
901/* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
902/* NOINLINE: limit stack usage in caller */
Denys Vlasenkoed898ed2017-03-27 22:32:44 +0200903static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6, struct d6_packet *d6_pkt, int fd)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100904{
905 int bytes;
906 struct ip6_udp_d6_packet packet;
907
908 bytes = safe_read(fd, &packet, sizeof(packet));
909 if (bytes < 0) {
James Byrne69374872019-07-02 11:35:03 +0200910 log1s("packet read error, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100911 /* NB: possible down interface, etc. Caller should pause. */
912 return bytes; /* returns -1 */
913 }
914
915 if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) {
James Byrne69374872019-07-02 11:35:03 +0200916 log1s("packet is too short, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100917 return -2;
918 }
919
920 if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) {
921 /* packet is bigger than sizeof(packet), we did partial read */
James Byrne69374872019-07-02 11:35:03 +0200922 log1s("oversized packet, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100923 return -2;
924 }
925
926 /* ignore any extra garbage bytes */
927 bytes = sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen);
928
929 /* make sure its the right packet for us, and that it passes sanity checks */
930 if (packet.ip6.ip6_nxt != IPPROTO_UDP
931 || (packet.ip6.ip6_vfc >> 4) != 6
Denys Vlasenko04ac6e02013-01-28 15:25:35 +0100932 || packet.udp.dest != htons(CLIENT_PORT6)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100933 /* || bytes > (int) sizeof(packet) - can't happen */
934 || packet.udp.len != packet.ip6.ip6_plen
935 ) {
James Byrne69374872019-07-02 11:35:03 +0200936 log1s("unrelated/bogus packet, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100937 return -2;
938 }
939
940//How to do this for ipv6?
941// /* verify UDP checksum. IP header has to be modified for this */
942// memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
943// /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
944// packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
945// check = packet.udp.check;
946// packet.udp.check = 0;
Denys Vlasenko4a0eb032020-10-01 03:07:22 +0200947// if (check && check != inet_cksum(&packet, bytes)) {
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200948// log1("packet with bad UDP checksum received, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100949// return -2;
950// }
951
Denys Vlasenkoed898ed2017-03-27 22:32:44 +0200952 if (peer_ipv6)
953 *peer_ipv6 = packet.ip6.ip6_src; /* struct copy */
954
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200955 log1("received %s", "a packet");
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100956 d6_dump_packet(&packet.data);
957
958 bytes -= sizeof(packet.ip6) + sizeof(packet.udp);
959 memcpy(d6_pkt, &packet.data, bytes);
960 return bytes;
961}
962
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100963/*** Main ***/
964
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200965/* Values for client_data.listen_mode */
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100966#define LISTEN_NONE 0
967#define LISTEN_KERNEL 1
968#define LISTEN_RAW 2
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100969
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +0200970/* Values for client_data.state */
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100971/* initial state: (re)start DHCP negotiation */
972#define INIT_SELECTING 0
973/* discover was sent, DHCPOFFER reply received */
974#define REQUESTING 1
975/* select/renew was sent, DHCPACK reply received */
976#define BOUND 2
977/* half of lease passed, want to renew it by sending unicast renew requests */
978#define RENEWING 3
979/* renew requests were not answered, lease is almost over, send broadcast renew */
980#define REBINDING 4
981/* manually requested renew (SIGUSR1) */
982#define RENEW_REQUESTED 5
983/* release, possibly manually requested (SIGUSR2) */
984#define RELEASED 6
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100985
986static int d6_raw_socket(int ifindex)
987{
988 int fd;
989 struct sockaddr_ll sock;
990
991 /*
992 * Comment:
993 *
994 * I've selected not to see LL header, so BPF doesn't see it, too.
995 * The filter may also pass non-IP and non-ARP packets, but we do
996 * a more complete check when receiving the message in userspace.
997 *
998 * and filter shamelessly stolen from:
999 *
1000 * http://www.flamewarmaster.de/software/dhcpclient/
1001 *
1002 * There are a few other interesting ideas on that page (look under
1003 * "Motivation"). Use of netlink events is most interesting. Think
1004 * of various network servers listening for events and reconfiguring.
1005 * That would obsolete sending HUP signals and/or make use of restarts.
1006 *
1007 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1008 * License: GPL v2.
1009 *
1010 * TODO: make conditional?
1011 */
1012#if 0
1013 static const struct sock_filter filter_instr[] = {
1014 /* load 9th byte (protocol) */
1015 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1016 /* jump to L1 if it is IPPROTO_UDP, else to L4 */
1017 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6),
1018 /* L1: load halfword from offset 6 (flags and frag offset) */
1019 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
1020 /* jump to L4 if any bits in frag offset field are set, else to L2 */
1021 BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0),
1022 /* L2: skip IP header (load index reg with header len) */
1023 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0),
1024 /* load udp destination port from halfword[header_len + 2] */
1025 BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2),
Denys Vlasenko04ac6e02013-01-28 15:25:35 +01001026 /* jump to L3 if udp dport is CLIENT_PORT6, else to L4 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001027 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1),
1028 /* L3: accept packet */
Denys Vlasenkoffc3a932014-02-19 14:17:11 +01001029 BPF_STMT(BPF_RET|BPF_K, 0x7fffffff),
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001030 /* L4: discard packet */
1031 BPF_STMT(BPF_RET|BPF_K, 0),
1032 };
1033 static const struct sock_fprog filter_prog = {
1034 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1035 /* casting const away: */
1036 .filter = (struct sock_filter *) filter_instr,
1037 };
1038#endif
1039
Denys Vlasenko168f0ef2017-07-21 12:04:22 +02001040 log2("opening raw socket on ifindex %d", ifindex);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001041
1042 fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
Denys Vlasenko6eb6e6a2019-05-31 23:51:07 +02001043 log3("got raw socket fd %d", fd);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001044
Denys Vlasenko2b9acc62017-09-29 14:09:02 +02001045 memset(&sock, 0, sizeof(sock)); /* let's be deterministic */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001046 sock.sll_family = AF_PACKET;
1047 sock.sll_protocol = htons(ETH_P_IPV6);
1048 sock.sll_ifindex = ifindex;
Denys Vlasenko2b9acc62017-09-29 14:09:02 +02001049 /*sock.sll_hatype = ARPHRD_???;*/
1050 /*sock.sll_pkttype = PACKET_???;*/
1051 /*sock.sll_halen = ???;*/
1052 /*sock.sll_addr[8] = ???;*/
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001053 xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
1054
1055#if 0
Denys Vlasenko04ac6e02013-01-28 15:25:35 +01001056 if (CLIENT_PORT6 == 546) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001057 /* Use only if standard port is in use */
1058 /* Ignoring error (kernel may lack support for this) */
1059 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1060 sizeof(filter_prog)) >= 0)
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001061 log1("attached filter to raw socket fd %d", fd); // log?
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001062 }
1063#endif
1064
James Byrne69374872019-07-02 11:35:03 +02001065 log1s("created raw socket");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001066
1067 return fd;
1068}
1069
1070static void change_listen_mode(int new_mode)
1071{
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001072 log1("entering listen mode: %s",
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001073 new_mode != LISTEN_NONE
1074 ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw")
1075 : "none"
1076 );
1077
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001078 client_data.listen_mode = new_mode;
1079 if (client_data.sockfd >= 0) {
1080 close(client_data.sockfd);
1081 client_data.sockfd = -1;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001082 }
1083 if (new_mode == LISTEN_KERNEL)
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001084 client_data.sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_data.interface);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001085 else if (new_mode != LISTEN_NONE)
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001086 client_data.sockfd = d6_raw_socket(client_data.ifindex);
1087 /* else LISTEN_NONE: client_data.sockfd stays closed */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001088}
1089
1090/* Called only on SIGUSR1 */
1091static void perform_renew(void)
1092{
James Byrne69374872019-07-02 11:35:03 +02001093 bb_simple_info_msg("performing DHCP renew");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001094 switch (client_data.state) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001095 case BOUND:
1096 change_listen_mode(LISTEN_KERNEL);
1097 case RENEWING:
1098 case REBINDING:
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001099 client_data.state = RENEW_REQUESTED;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001100 break;
1101 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
David Decotigny8f48fc02018-05-24 08:30:15 -07001102 d6_run_script_no_option("deconfig");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001103 case REQUESTING:
1104 case RELEASED:
1105 change_listen_mode(LISTEN_RAW);
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001106 client_data.state = INIT_SELECTING;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001107 break;
1108 case INIT_SELECTING:
1109 break;
1110 }
1111}
1112
1113static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
1114{
1115 /* send release packet */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001116 if (client_data.state == BOUND
1117 || client_data.state == RENEWING
1118 || client_data.state == REBINDING
1119 || client_data.state == RENEW_REQUESTED
Denys Vlasenko44399e02016-07-03 20:26:44 +02001120 ) {
James Byrne69374872019-07-02 11:35:03 +02001121 bb_simple_info_msg("unicasting a release");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001122 send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001123 }
James Byrne69374872019-07-02 11:35:03 +02001124 bb_simple_info_msg("entering released state");
Peter Korsgaardb6355e22016-08-26 18:46:34 +02001125/*
1126 * We can be here on: SIGUSR2,
1127 * or on exit (SIGTERM) and -R "release on quit" is specified.
1128 * Users requested to be notified in all cases, even if not in one
1129 * of the states above.
1130 */
David Decotigny8f48fc02018-05-24 08:30:15 -07001131 d6_run_script_no_option("deconfig");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001132 change_listen_mode(LISTEN_NONE);
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001133 client_data.state = RELEASED;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001134}
1135
1136///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
1137///{
1138/// uint8_t *storage;
1139/// int len = strnlen(str, 255);
1140/// storage = xzalloc(len + extra + OPT_DATA);
1141/// storage[OPT_CODE] = code;
1142/// storage[OPT_LEN] = len + extra;
1143/// memcpy(storage + extra + OPT_DATA, str, len);
1144/// return storage;
1145///}
1146
1147#if BB_MMU
1148static void client_background(void)
1149{
1150 bb_daemonize(0);
1151 logmode &= ~LOGMODE_STDIO;
1152 /* rewrite pidfile, as our pid is different now */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001153 write_pidfile(client_data.pidfile);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001154}
1155#endif
1156
1157//usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
1158//usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__
1159//usage:#else
1160//usage:# define IF_UDHCP_VERBOSE(...)
1161//usage:#endif
1162//usage:#define udhcpc6_trivial_usage
Denys Vlasenko64211ce2018-01-16 22:23:38 +01001163//usage: "[-fbnq"IF_UDHCP_VERBOSE("v")"odR] [-i IFACE] [-r IPv6] [-s PROG] [-p PIDFILE]\n"
Denys Vlasenko8cc3a742020-12-18 22:51:46 +01001164//usage: " [-x OPT:VAL]... [-O OPT]..." IF_FEATURE_UDHCP_PORT(" [-P PORT]")
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001165//usage:#define udhcpc6_full_usage "\n"
Andre Kalb5f2e00f2020-05-05 18:24:25 +02001166//usage: "\n -i IFACE Interface to use (default "CONFIG_UDHCPC_DEFAULT_INTERFACE")"
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001167//usage: "\n -p FILE Create pidfile"
1168//usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC_DEFAULT_SCRIPT")"
1169//usage: "\n -B Request broadcast replies"
1170//usage: "\n -t N Send up to N discover packets"
1171//usage: "\n -T N Pause between packets (default 3 seconds)"
1172//usage: "\n -A N Wait N seconds (default 20) after failure"
1173//usage: "\n -f Run in foreground"
1174//usage: USE_FOR_MMU(
1175//usage: "\n -b Background if lease is not obtained"
1176//usage: )
1177//usage: "\n -n Exit if lease is not obtained"
1178//usage: "\n -q Exit after obtaining lease"
1179//usage: "\n -R Release IP on exit"
1180//usage: "\n -S Log to syslog too"
1181//usage: IF_FEATURE_UDHCP_PORT(
Denys Vlasenko8cc3a742020-12-18 22:51:46 +01001182//usage: "\n -P PORT Use PORT (default 546)"
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001183//usage: )
1184////usage: IF_FEATURE_UDHCPC_ARPING(
1185////usage: "\n -a Use arping to validate offered address"
1186////usage: )
1187//usage: "\n -O OPT Request option OPT from server (cumulative)"
1188//usage: "\n -o Don't request any options (unless -O is given)"
Denys Vlasenko64211ce2018-01-16 22:23:38 +01001189//usage: "\n -r IPv6 Request this address ('no' to not request any IP)"
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001190//usage: "\n -d Request prefix"
Eivind Versvik22a63392019-08-24 17:23:48 +02001191//usage: "\n -l Send 'information request' instead of 'solicit'"
1192//usage: "\n (used for servers which do not assign IPv6 addresses)"
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001193//usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)"
1194//usage: "\n Examples of string, numeric, and hex byte opts:"
1195//usage: "\n -x hostname:bbox - option 12"
1196//usage: "\n -x lease:3600 - option 51 (lease time)"
1197//usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)"
Denys Vlasenko266f6f12018-04-13 13:18:34 +02001198//usage: "\n -x 14:'\"dumpfile\"' - option 14 (shell-quoted)"
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001199//usage: IF_UDHCP_VERBOSE(
1200//usage: "\n -v Verbose"
1201//usage: )
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001202//usage: "\nSignals:"
1203//usage: "\n USR1 Renew lease"
1204//usage: "\n USR2 Release lease"
1205
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001206int udhcpc6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1207int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1208{
1209 const char *str_r;
1210 IF_FEATURE_UDHCP_PORT(char *str_P;)
1211 void *clientid_mac_ptr;
1212 llist_t *list_O = NULL;
1213 llist_t *list_x = NULL;
1214 int tryagain_timeout = 20;
1215 int discover_timeout = 3;
1216 int discover_retries = 3;
1217 struct in6_addr srv6_buf;
1218 struct in6_addr ipv6_buf;
1219 struct in6_addr *requested_ipv6;
1220 uint32_t xid = 0;
1221 int packet_num;
1222 int timeout; /* must be signed */
1223 unsigned already_waited_sec;
1224 unsigned opt;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001225 int retval;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001226
Denys Vlasenkodf70a432016-04-21 18:54:36 +02001227 setup_common_bufsiz();
Denys Vlasenkof6dd9e02018-01-19 18:44:19 +01001228 /* We want random_xid to be random */
1229 srand(monotonic_us());
Denys Vlasenkodf70a432016-04-21 18:54:36 +02001230
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001231 /* Default options */
Denys Vlasenko04ac6e02013-01-28 15:25:35 +01001232 IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;)
1233 IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;)
Andre Kalb5f2e00f2020-05-05 18:24:25 +02001234 client_data.interface = CONFIG_UDHCPC_DEFAULT_INTERFACE;
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001235 client_data.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
1236 client_data.sockfd = -1;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001237
Denys Vlasenko65c34c52019-05-31 23:39:22 +02001238 /* Make sure fd 0,1,2 are open */
1239 /* Set up the signal pipe on fds 3,4 - must be before openlog() */
1240 udhcp_sp_setup();
1241
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001242 /* Parse command line */
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001243 opt = getopt32long(argv, "^"
1244 /* O,x: list; -T,-t,-A take numeric param */
Eivind Versvik22a63392019-08-24 17:23:48 +02001245 "i:np:qRr:s:T:+t:+SA:+O:*ox:*fld"
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001246 USE_FOR_MMU("b")
1247 ///IF_FEATURE_UDHCPC_ARPING("a")
1248 IF_FEATURE_UDHCP_PORT("P:")
1249 "v"
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001250 "\0" IF_UDHCP_VERBOSE("vv") /* -v is a counter */
Denys Vlasenko036585a2017-08-08 16:38:18 +02001251 , udhcpc6_longopts
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001252 , &client_data.interface, &client_data.pidfile, &str_r /* i,p */
1253 , &client_data.script /* s */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001254 , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
1255 , &list_O
1256 , &list_x
1257 IF_FEATURE_UDHCP_PORT(, &str_P)
1258 IF_UDHCP_VERBOSE(, &dhcp_verbose)
Denys Vlasenko7e21f042011-11-08 11:39:41 +01001259 );
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001260 requested_ipv6 = NULL;
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001261 option_mask32 |= OPT_r;
Eivind Versvik22a63392019-08-24 17:23:48 +02001262 if (opt & OPT_l) {
1263 /* for -l, do not require IPv6 assignment from server */
1264 option_mask32 &= ~OPT_r;
1265 } else if (opt & OPT_r) {
1266 /* explicit "-r ARG" given */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001267 if (strcmp(str_r, "no") == 0) {
Eivind Versvik22a63392019-08-24 17:23:48 +02001268 option_mask32 &= ~OPT_r;
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001269 } else {
1270 if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0)
1271 bb_error_msg_and_die("bad IPv6 address '%s'", str_r);
1272 requested_ipv6 = &ipv6_buf;
1273 }
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001274 }
Eivind Versvik22a63392019-08-24 17:23:48 +02001275
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001276#if ENABLE_FEATURE_UDHCP_PORT
1277 if (opt & OPT_P) {
Denys Vlasenko04ac6e02013-01-28 15:25:35 +01001278 CLIENT_PORT6 = xatou16(str_P);
1279 SERVER_PORT6 = CLIENT_PORT6 + 1;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001280 }
1281#endif
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001282 while (list_O) {
1283 char *optstr = llist_pop(&list_O);
1284 unsigned n = bb_strtou(optstr, NULL, 0);
1285 if (errno || n > 254) {
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +02001286 n = udhcp_option_idx(optstr, d6_option_strings);
1287 n = d6_optflags[n].code;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001288 }
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001289 client_data.opt_mask[n >> 3] |= 1 << (n & 7);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001290 }
Denys Vlasenko293c9452012-07-27 13:25:07 +02001291 if (!(opt & OPT_o)) {
Denys Vlasenko293c9452012-07-27 13:25:07 +02001292 unsigned i, n;
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +02001293 for (i = 0; (n = d6_optflags[i].code) != 0; i++) {
1294 if (d6_optflags[i].flags & OPTION_REQ) {
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001295 client_data.opt_mask[n >> 3] |= 1 << (n & 7);
Denys Vlasenko293c9452012-07-27 13:25:07 +02001296 }
1297 }
Denys Vlasenko293c9452012-07-27 13:25:07 +02001298 }
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001299 while (list_x) {
Denys Vlasenko266f6f12018-04-13 13:18:34 +02001300 char *optstr = xstrdup(llist_pop(&list_x));
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001301 udhcp_str2optset(optstr, &client_data.options,
Denys Vlasenko60275972018-05-14 11:06:35 +02001302 d6_optflags, d6_option_strings,
1303 /*dhcpv6:*/ 1
1304 );
Denys Vlasenko266f6f12018-04-13 13:18:34 +02001305 free(optstr);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001306 }
1307
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001308 if (d6_read_interface(client_data.interface,
1309 &client_data.ifindex,
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +02001310 &client6_data.ll_ip6,
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001311 client_data.client_mac)
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001312 ) {
1313 return 1;
1314 }
1315
1316 /* Create client ID based on mac, set clientid_mac_ptr */
1317 {
1318 struct d6_option *clientid;
1319 clientid = xzalloc(2+2+2+2+6);
1320 clientid->code = D6_OPT_CLIENTID;
1321 clientid->len = 2+2+6;
Denys Vlasenko68c5b282011-11-07 16:21:24 +01001322 clientid->data[1] = 3; /* DUID-LL */
1323 clientid->data[3] = 1; /* ethernet */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001324 clientid_mac_ptr = clientid->data + 2+2;
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001325 memcpy(clientid_mac_ptr, client_data.client_mac, 6);
1326 client_data.clientid = (void*)clientid;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001327 }
1328
1329#if !BB_MMU
1330 /* on NOMMU reexec (i.e., background) early */
1331 if (!(opt & OPT_f)) {
1332 bb_daemonize_or_rexec(0 /* flags */, argv);
1333 logmode = LOGMODE_NONE;
1334 }
1335#endif
1336 if (opt & OPT_S) {
1337 openlog(applet_name, LOG_PID, LOG_DAEMON);
1338 logmode |= LOGMODE_SYSLOG;
1339 }
1340
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001341 /* Create pidfile */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001342 write_pidfile(client_data.pidfile);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001343 /* Goes to stdout (unless NOMMU) and possibly syslog */
James Byrne69374872019-07-02 11:35:03 +02001344 bb_simple_info_msg("started, v"BB_VER);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001345
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001346 client_data.state = INIT_SELECTING;
David Decotigny8f48fc02018-05-24 08:30:15 -07001347 d6_run_script_no_option("deconfig");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001348 change_listen_mode(LISTEN_RAW);
1349 packet_num = 0;
1350 timeout = 0;
1351 already_waited_sec = 0;
1352
1353 /* Main event loop. select() waits on signal pipe and possibly
1354 * on sockfd.
1355 * "continue" statements in code below jump to the top of the loop.
1356 */
1357 for (;;) {
Denys Vlasenko52a515d2017-02-16 23:25:44 +01001358 int tv;
1359 struct pollfd pfds[2];
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001360 struct d6_packet packet;
1361 uint8_t *packet_end;
1362 /* silence "uninitialized!" warning */
1363 unsigned timestamp_before_wait = timestamp_before_wait;
1364
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001365 //bb_error_msg("sockfd:%d, listen_mode:%d", client_data.sockfd, client_data.listen_mode);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001366
1367 /* Was opening raw or udp socket here
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001368 * if (client_data.listen_mode != LISTEN_NONE && client_data.sockfd < 0),
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001369 * but on fast network renew responses return faster
1370 * than we open sockets. Thus this code is moved
1371 * to change_listen_mode(). Thus we open listen socket
1372 * BEFORE we send renew request (see "case BOUND:"). */
1373
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001374 udhcp_sp_fd_set(pfds, client_data.sockfd);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001375
Denys Vlasenko52a515d2017-02-16 23:25:44 +01001376 tv = timeout - already_waited_sec;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001377 retval = 0;
1378 /* If we already timed out, fall through with retval = 0, else... */
Denys Vlasenko52a515d2017-02-16 23:25:44 +01001379 if (tv > 0) {
Denys Vlasenkode6cb412017-07-24 12:01:28 +02001380 log1("waiting %u seconds", tv);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001381 timestamp_before_wait = (unsigned)monotonic_sec();
Denys Vlasenko7c67f1e2017-02-17 19:20:32 +01001382 retval = poll(pfds, 2, tv < INT_MAX/1000 ? tv * 1000 : INT_MAX);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001383 if (retval < 0) {
1384 /* EINTR? A signal was caught, don't panic */
1385 if (errno == EINTR) {
1386 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
1387 continue;
1388 }
1389 /* Else: an error occured, panic! */
James Byrne69374872019-07-02 11:35:03 +02001390 bb_simple_perror_msg_and_die("poll");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001391 }
1392 }
1393
1394 /* If timeout dropped to zero, time to become active:
1395 * resend discover/renew/whatever
1396 */
1397 if (retval == 0) {
1398 /* When running on a bridge, the ifindex may have changed
1399 * (e.g. if member interfaces were added/removed
1400 * or if the status of the bridge changed).
1401 * Refresh ifindex and client_mac:
1402 */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001403 if (d6_read_interface(client_data.interface,
1404 &client_data.ifindex,
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +02001405 &client6_data.ll_ip6,
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001406 client_data.client_mac)
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001407 ) {
1408 goto ret0; /* iface is gone? */
1409 }
Denys Vlasenkoe09f5e32017-03-27 22:10:15 +02001410
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001411 memcpy(clientid_mac_ptr, client_data.client_mac, 6);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001412
1413 /* We will restart the wait in any case */
1414 already_waited_sec = 0;
1415
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001416 switch (client_data.state) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001417 case INIT_SELECTING:
Felix Fietkau1c7a58d2012-09-27 16:22:24 +02001418 if (!discover_retries || packet_num < discover_retries) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001419 if (packet_num == 0)
1420 xid = random_xid();
1421 /* multicast */
Eivind Versvik22a63392019-08-24 17:23:48 +02001422 if (opt & OPT_l)
1423 send_d6_info_request(xid);
1424 else
1425 send_d6_discover(xid, requested_ipv6);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001426 timeout = discover_timeout;
1427 packet_num++;
1428 continue;
1429 }
1430 leasefail:
David Decotigny8f48fc02018-05-24 08:30:15 -07001431 d6_run_script_no_option("leasefail");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001432#if BB_MMU /* -b is not supported on NOMMU */
1433 if (opt & OPT_b) { /* background if no lease */
James Byrne69374872019-07-02 11:35:03 +02001434 bb_simple_info_msg("no lease, forking to background");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001435 client_background();
1436 /* do not background again! */
Andrey Mazo87e21622019-02-22 16:46:19 -05001437 opt = ((opt & ~(OPT_b|OPT_n)) | OPT_f);
1438 /* ^^^ also disables -n (-b takes priority over -n):
1439 * ifup's default udhcpc options are -R -n,
1440 * and users want to be able to add -b
1441 * (in a config file) to make it background
1442 * _and not exit_.
1443 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001444 } else
1445#endif
1446 if (opt & OPT_n) { /* abort if no lease */
James Byrne69374872019-07-02 11:35:03 +02001447 bb_simple_info_msg("no lease, failing");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001448 retval = 1;
1449 goto ret;
1450 }
1451 /* wait before trying again */
1452 timeout = tryagain_timeout;
1453 packet_num = 0;
1454 continue;
1455 case REQUESTING:
Felix Fietkau1c7a58d2012-09-27 16:22:24 +02001456 if (!discover_retries || packet_num < discover_retries) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001457 /* send multicast select packet */
1458 send_d6_select(xid);
1459 timeout = discover_timeout;
1460 packet_num++;
1461 continue;
1462 }
1463 /* Timed out, go back to init state.
1464 * "discover...select...discover..." loops
1465 * were seen in the wild. Treat them similarly
1466 * to "no response to discover" case */
1467 change_listen_mode(LISTEN_RAW);
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001468 client_data.state = INIT_SELECTING;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001469 goto leasefail;
1470 case BOUND:
1471 /* 1/2 lease passed, enter renewing state */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001472 client_data.state = RENEWING;
1473 client_data.first_secs = 0; /* make secs field count from 0 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001474 change_listen_mode(LISTEN_KERNEL);
James Byrne69374872019-07-02 11:35:03 +02001475 log1s("entering renew state");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001476 /* fall right through */
1477 case RENEW_REQUESTED: /* manual (SIGUSR1) renew */
1478 case_RENEW_REQUESTED:
1479 case RENEWING:
Denys Vlasenkoaae428f2018-09-28 14:44:09 +02001480 if (timeout >= 60) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001481 /* send an unicast renew request */
1482 /* Sometimes observed to fail (EADDRNOTAVAIL) to bind
1483 * a new UDP socket for sending inside send_renew.
1484 * I hazard to guess existing listening socket
1485 * is somehow conflicting with it, but why is it
1486 * not deterministic then?! Strange.
1487 * Anyway, it does recover by eventually failing through
1488 * into INIT_SELECTING state.
1489 */
Eivind Versvik22a63392019-08-24 17:23:48 +02001490 if (opt & OPT_l)
1491 send_d6_info_request(xid);
1492 else
1493 send_d6_renew(xid, &srv6_buf, requested_ipv6);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001494 timeout >>= 1;
1495 continue;
1496 }
1497 /* Timed out, enter rebinding state */
James Byrne69374872019-07-02 11:35:03 +02001498 log1s("entering rebinding state");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001499 client_data.state = REBINDING;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001500 /* fall right through */
1501 case REBINDING:
1502 /* Switch to bcast receive */
1503 change_listen_mode(LISTEN_RAW);
1504 /* Lease is *really* about to run out,
1505 * try to find DHCP server using broadcast */
1506 if (timeout > 0) {
Eivind Versvik22a63392019-08-24 17:23:48 +02001507 if (opt & OPT_l)
1508 send_d6_info_request(xid);
1509 else /* send a broadcast renew request */
1510 send_d6_renew(xid, /*server_ipv6:*/ NULL, requested_ipv6);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001511 timeout >>= 1;
1512 continue;
1513 }
1514 /* Timed out, enter init state */
James Byrne69374872019-07-02 11:35:03 +02001515 bb_simple_info_msg("lease lost, entering init state");
David Decotigny8f48fc02018-05-24 08:30:15 -07001516 d6_run_script_no_option("deconfig");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001517 client_data.state = INIT_SELECTING;
1518 client_data.first_secs = 0; /* make secs field count from 0 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001519 /*timeout = 0; - already is */
1520 packet_num = 0;
1521 continue;
1522 /* case RELEASED: */
1523 }
1524 /* yah, I know, *you* say it would never happen */
1525 timeout = INT_MAX;
1526 continue; /* back to main loop */
Denys Vlasenko3293bc12018-03-10 19:01:48 +01001527 } /* if poll timed out */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001528
Denys Vlasenko3293bc12018-03-10 19:01:48 +01001529 /* poll() didn't timeout, something happened */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001530
1531 /* Is it a signal? */
Denys Vlasenko3293bc12018-03-10 19:01:48 +01001532 switch (udhcp_sp_read()) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001533 case SIGUSR1:
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001534 client_data.first_secs = 0; /* make secs field count from 0 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001535 already_waited_sec = 0;
1536 perform_renew();
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001537 if (client_data.state == RENEW_REQUESTED) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001538 /* We might be either on the same network
1539 * (in which case renew might work),
1540 * or we might be on a completely different one
1541 * (in which case renew won't ever succeed).
1542 * For the second case, must make sure timeout
1543 * is not too big, or else we can send
1544 * futile renew requests for hours.
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001545 */
Denys Vlasenkoaae428f2018-09-28 14:44:09 +02001546 if (timeout > 60)
1547 timeout = 60;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001548 goto case_RENEW_REQUESTED;
1549 }
1550 /* Start things over */
1551 packet_num = 0;
1552 /* Kill any timeouts, user wants this to hurry along */
1553 timeout = 0;
1554 continue;
1555 case SIGUSR2:
1556 perform_d6_release(&srv6_buf, requested_ipv6);
1557 timeout = INT_MAX;
1558 continue;
1559 case SIGTERM:
James Byrne253c4e72019-04-12 17:01:51 +00001560 bb_info_msg("received %s", "SIGTERM");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001561 goto ret0;
1562 }
1563
1564 /* Is it a packet? */
Denys Vlasenko3293bc12018-03-10 19:01:48 +01001565 if (!pfds[1].revents)
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001566 continue; /* no */
1567
1568 {
1569 int len;
1570
1571 /* A packet is ready, read it */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001572 if (client_data.listen_mode == LISTEN_KERNEL)
1573 len = d6_recv_kernel_packet(&srv6_buf, &packet, client_data.sockfd);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001574 else
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001575 len = d6_recv_raw_packet(&srv6_buf, &packet, client_data.sockfd);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001576 if (len == -1) {
1577 /* Error is severe, reopen socket */
Denys Vlasenko6f97b302017-09-29 18:17:25 +02001578 bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001579 sleep(discover_timeout); /* 3 seconds by default */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001580 change_listen_mode(client_data.listen_mode); /* just close and reopen */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001581 }
1582 /* If this packet will turn out to be unrelated/bogus,
1583 * we will go back and wait for next one.
1584 * Be sure timeout is properly decreased. */
1585 already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait;
1586 if (len < 0)
1587 continue;
1588 packet_end = (uint8_t*)&packet + len;
1589 }
1590
1591 if ((packet.d6_xid32 & htonl(0x00ffffff)) != xid) {
Denys Vlasenkoa4959ee2021-02-21 16:32:07 +01001592 log1("xid %x (our is %x)%s",
1593 (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)xid,
1594 ", ignoring packet"
1595 );
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001596 continue;
1597 }
1598
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001599 switch (client_data.state) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001600 case INIT_SELECTING:
1601 if (packet.d6_msg_type == D6_MSG_ADVERTISE)
1602 goto type_is_ok;
1603 /* DHCPv6 has "Rapid Commit", when instead of Advertise,
1604 * server sends Reply right away.
1605 * Fall through to check for this case.
1606 */
1607 case REQUESTING:
1608 case RENEWING:
1609 case RENEW_REQUESTED:
1610 case REBINDING:
1611 if (packet.d6_msg_type == D6_MSG_REPLY) {
1612 uint32_t lease_seconds;
Denys Vlasenko14f13202018-01-16 21:47:10 +01001613 struct d6_option *option;
Denys Vlasenko7c44b602018-01-17 13:55:51 +01001614 unsigned address_timeout;
1615 unsigned prefix_timeout;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001616 type_is_ok:
Denys Vlasenko14f13202018-01-16 21:47:10 +01001617 address_timeout = 0;
1618 prefix_timeout = 0;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001619 option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE);
Denys Vlasenko64d58aa2017-03-27 22:22:09 +02001620 if (option && (option->data[0] | option->data[1]) != 0) {
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001621 /* return to init state */
James Byrne253c4e72019-04-12 17:01:51 +00001622 bb_info_msg("received DHCP NAK (%u)", option->data[4]);
David Decotigny8f48fc02018-05-24 08:30:15 -07001623 d6_run_script(packet.d6_options,
1624 packet_end, "nak");
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001625 if (client_data.state != REQUESTING)
David Decotigny8f48fc02018-05-24 08:30:15 -07001626 d6_run_script_no_option("deconfig");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001627 change_listen_mode(LISTEN_RAW);
1628 sleep(3); /* avoid excessive network traffic */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001629 client_data.state = INIT_SELECTING;
1630 client_data.first_secs = 0; /* make secs field count from 0 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001631 requested_ipv6 = NULL;
1632 timeout = 0;
1633 packet_num = 0;
1634 already_waited_sec = 0;
1635 continue;
1636 }
1637 option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID);
1638 if (!option) {
James Byrne69374872019-07-02 11:35:03 +02001639 bb_simple_info_msg("no server ID, ignoring packet");
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001640 continue;
1641 /* still selecting - this server looks bad */
1642 }
1643//Note: we do not bother comparing server IDs in Advertise and Reply msgs.
1644//server_id variable is used solely for creation of proper server_id option
1645//in outgoing packets. (why DHCPv6 even introduced it is a mystery).
1646 free(client6_data.server_id);
1647 client6_data.server_id = option;
1648 if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
1649 /* enter requesting state */
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001650 client_data.state = REQUESTING;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001651 timeout = 0;
1652 packet_num = 0;
1653 already_waited_sec = 0;
1654 continue;
1655 }
1656 /* It's a D6_MSG_REPLY */
1657/*
1658 * RFC 3315 18.1.8. Receipt of Reply Messages
1659 *
1660 * Upon the receipt of a valid Reply message in response to a Solicit
1661 * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or
1662 * Information-request message, the client extracts the configuration
1663 * information contained in the Reply. The client MAY choose to report
1664 * any status code or message from the status code option in the Reply
1665 * message.
1666 *
1667 * The client SHOULD perform duplicate address detection [17] on each of
1668 * the addresses in any IAs it receives in the Reply message before
1669 * using that address for traffic. If any of the addresses are found to
1670 * be in use on the link, the client sends a Decline message to the
1671 * server as described in section 18.1.7.
1672 *
1673 * If the Reply was received in response to a Solicit (with a Rapid
1674 * Commit option), Request, Renew or Rebind message, the client updates
1675 * the information it has recorded about IAs from the IA options
1676 * contained in the Reply message:
1677 *
1678 * - Record T1 and T2 times.
1679 *
1680 * - Add any new addresses in the IA option to the IA as recorded by
1681 * the client.
1682 *
1683 * - Update lifetimes for any addresses in the IA option that the
1684 * client already has recorded in the IA.
1685 *
1686 * - Discard any addresses from the IA, as recorded by the client, that
1687 * have a valid lifetime of 0 in the IA Address option.
1688 *
1689 * - Leave unchanged any information about addresses the client has
1690 * recorded in the IA but that were not included in the IA from the
1691 * server.
1692 *
1693 * Management of the specific configuration information is detailed in
1694 * the definition of each option in section 22.
1695 *
1696 * If the client receives a Reply message with a Status Code containing
1697 * UnspecFail, the server is indicating that it was unable to process
1698 * the message due to an unspecified failure condition. If the client
1699 * retransmits the original message to the same server to retry the
1700 * desired operation, the client MUST limit the rate at which it
1701 * retransmits the message and limit the duration of the time during
1702 * which it retransmits the message.
1703 *
1704 * When the client receives a Reply message with a Status Code option
1705 * with the value UseMulticast, the client records the receipt of the
1706 * message and sends subsequent messages to the server through the
1707 * interface on which the message was received using multicast. The
1708 * client resends the original message using multicast.
1709 *
1710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1711 * | OPTION_IA_NA | option-len |
1712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1713 * | IAID (4 octets) |
1714 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1715 * | T1 |
1716 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1717 * | T2 |
1718 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1719 * | |
1720 * . IA_NA-options .
1721 * . .
1722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1723 *
1724 *
1725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1726 * | OPTION_IAADDR | option-len |
1727 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1728 * | |
1729 * | IPv6 address |
1730 * | |
1731 * | |
1732 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1733 * | preferred-lifetime |
1734 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1735 * | valid-lifetime |
1736 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1737 * . .
1738 * . IAaddr-options .
1739 * . .
1740 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1741 */
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001742 if (option_mask32 & OPT_r) {
Denys Vlasenko14f13202018-01-16 21:47:10 +01001743 struct d6_option *iaaddr;
1744
Denys Vlasenko1e8d79b2018-01-16 21:35:39 +01001745 free(client6_data.ia_na);
1746 client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA);
1747 if (!client6_data.ia_na) {
Denys Vlasenkoa4959ee2021-02-21 16:32:07 +01001748 bb_info_msg("no %s option%s", "IA_NA", ", ignoring packet");
Denys Vlasenko1e8d79b2018-01-16 21:35:39 +01001749 continue;
1750 }
1751 if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) {
James Byrne253c4e72019-04-12 17:01:51 +00001752 bb_info_msg("%s option is too short:%d bytes",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001753 "IA_NA", client6_data.ia_na->len);
Denys Vlasenko1e8d79b2018-01-16 21:35:39 +01001754 continue;
1755 }
1756 iaaddr = d6_find_option(client6_data.ia_na->data + 4 + 4 + 4,
1757 client6_data.ia_na->data + client6_data.ia_na->len,
1758 D6_OPT_IAADDR
1759 );
1760 if (!iaaddr) {
Denys Vlasenkoa4959ee2021-02-21 16:32:07 +01001761 bb_info_msg("no %s option%s", "IAADDR", ", ignoring packet");
Denys Vlasenko1e8d79b2018-01-16 21:35:39 +01001762 continue;
1763 }
1764 if (iaaddr->len < (16 + 4 + 4)) {
James Byrne253c4e72019-04-12 17:01:51 +00001765 bb_info_msg("%s option is too short:%d bytes",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001766 "IAADDR", iaaddr->len);
Denys Vlasenko1e8d79b2018-01-16 21:35:39 +01001767 continue;
1768 }
1769 /* Note: the address is sufficiently aligned for cast:
1770 * we _copied_ IA-NA, and copy is always well-aligned.
1771 */
1772 requested_ipv6 = (struct in6_addr*) iaaddr->data;
1773 move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4);
1774 lease_seconds = ntohl(lease_seconds);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001775/// TODO: check for 0 lease time?
James Byrne253c4e72019-04-12 17:01:51 +00001776 bb_info_msg("%s obtained, lease time %u",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001777 "IPv6", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
Denys Vlasenko7c44b602018-01-17 13:55:51 +01001778 address_timeout = lease_seconds;
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001779 }
1780 if (option_mask32 & OPT_d) {
1781 struct d6_option *iaprefix;
1782
1783 free(client6_data.ia_pd);
1784 client6_data.ia_pd = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_PD);
1785 if (!client6_data.ia_pd) {
Denys Vlasenkoa4959ee2021-02-21 16:32:07 +01001786 bb_info_msg("no %s option%s", "IA_PD", ", ignoring packet");
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001787 continue;
1788 }
1789 if (client6_data.ia_pd->len < (4 + 4 + 4) + (2 + 2 + 4 + 4 + 1 + 16)) {
James Byrne253c4e72019-04-12 17:01:51 +00001790 bb_info_msg("%s option is too short:%d bytes",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001791 "IA_PD", client6_data.ia_pd->len);
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001792 continue;
1793 }
1794 iaprefix = d6_find_option(client6_data.ia_pd->data + 4 + 4 + 4,
1795 client6_data.ia_pd->data + client6_data.ia_pd->len,
1796 D6_OPT_IAPREFIX
1797 );
1798 if (!iaprefix) {
Denys Vlasenkoa4959ee2021-02-21 16:32:07 +01001799 bb_info_msg("no %s option%s", "IAPREFIX", ", ignoring packet");
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001800 continue;
1801 }
1802 if (iaprefix->len < (4 + 4 + 1 + 16)) {
James Byrne253c4e72019-04-12 17:01:51 +00001803 bb_info_msg("%s option is too short:%d bytes",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001804 "IAPREFIX", iaprefix->len);
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001805 continue;
1806 }
1807 move_from_unaligned32(lease_seconds, iaprefix->data + 4);
1808 lease_seconds = ntohl(lease_seconds);
James Byrne253c4e72019-04-12 17:01:51 +00001809 bb_info_msg("%s obtained, lease time %u",
Denys Vlasenko14f13202018-01-16 21:47:10 +01001810 "prefix", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds);
Denys Vlasenko7c44b602018-01-17 13:55:51 +01001811 prefix_timeout = lease_seconds;
Denys Vlasenkoef5207f2018-01-16 21:39:14 +01001812 }
Denys Vlasenko6e9e6d82018-01-16 21:52:23 +01001813 if (!address_timeout)
1814 address_timeout = prefix_timeout;
1815 if (!prefix_timeout)
1816 prefix_timeout = address_timeout;
Denys Vlasenko7c44b602018-01-17 13:55:51 +01001817 /* note: "int timeout" will not overflow even with 0xffffffff inputs here: */
1818 timeout = (prefix_timeout < address_timeout ? prefix_timeout : address_timeout) / 2;
Eivind Versvik22a63392019-08-24 17:23:48 +02001819 if (opt & OPT_l) {
1820 /* TODO: request OPTION_INFORMATION_REFRESH_TIME (32)
1821 * and use its value instead of the default 1 day.
1822 */
1823 timeout = 24 * 60 * 60;
1824 }
Denys Vlasenko14f13202018-01-16 21:47:10 +01001825 /* paranoia: must not be too small */
Denys Vlasenkoc05aa6a2018-10-30 11:56:18 +01001826 /* timeout > 60 - ensures at least one unicast renew attempt */
1827 if (timeout < 61)
1828 timeout = 61;
Denys Vlasenko14f13202018-01-16 21:47:10 +01001829 /* enter bound state */
David Decotigny8f48fc02018-05-24 08:30:15 -07001830 d6_run_script(packet.d6_options, packet_end,
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001831 (client_data.state == REQUESTING ? "bound" : "renew"));
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001832
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001833 client_data.state = BOUND;
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001834 change_listen_mode(LISTEN_NONE);
1835 if (opt & OPT_q) { /* quit after lease */
1836 goto ret0;
1837 }
1838 /* future renew failures should not exit (JM) */
1839 opt &= ~OPT_n;
1840#if BB_MMU /* NOMMU case backgrounded earlier */
1841 if (!(opt & OPT_f)) {
1842 client_background();
1843 /* do not background again! */
1844 opt = ((opt & ~OPT_b) | OPT_f);
1845 }
1846#endif
1847 already_waited_sec = 0;
1848 continue; /* back to main loop */
1849 }
1850 continue;
1851 /* case BOUND: - ignore all packets */
1852 /* case RELEASED: - ignore all packets */
1853 }
1854 /* back to main loop */
1855 } /* for (;;) - main loop ends */
1856
1857 ret0:
1858 if (opt & OPT_R) /* release on quit */
1859 perform_d6_release(&srv6_buf, requested_ipv6);
1860 retval = 0;
1861 ret:
Denys Vlasenkobcb1fc32019-05-26 15:01:13 +02001862 /*if (client_data.pidfile) - remove_pidfile has its own check */
1863 remove_pidfile(client_data.pidfile);
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001864 return retval;
Denys Vlasenkoabe248b2019-05-15 14:19:46 +02001865}