Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * DHCPv6 client. |
| 4 | * |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 5 | * Copyright (C) 2011-2017 Denys Vlasenko. |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 8 | */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 9 | //config:config UDHCPC6 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "udhcpc6 (21 kb)" |
Denys Vlasenko | cc45cbc | 2019-04-13 17:32:40 +0200 | [diff] [blame] | 11 | //config: default y |
Mike Frysinger | 3da46c8 | 2012-05-02 21:45:35 -0400 | [diff] [blame] | 12 | //config: depends on FEATURE_IPV6 |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: udhcpc6 is a DHCPv6 client |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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". |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 30 | //config: You can request FQDN to be given by server using "-O fqdn". |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 37 | //config: You can request POSIX timezone with "-O tz" and timezone name |
| 38 | //config: with "-O timezone". |
Samuel Mendoza-Jonas | 23cbd7d | 2018-05-14 14:29:12 +1000 | [diff] [blame] | 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". |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 47 | |
| 48 | //applet:IF_UDHCPC6(APPLET(udhcpc6, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 49 | |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 50 | //kbuild:lib-$(CONFIG_UDHCPC6) += d6_dhcpc.o d6_packet.o d6_socket.o common.o socket.o signalpipe.o |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 51 | //kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC3646) += domain_codec.o |
| 52 | //kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC4704) += domain_codec.o |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 53 | |
| 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 Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 66 | /* "struct client_data_t client_data" is in bb_common_bufsiz1 */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 67 | |
Denys Vlasenko | ca466f3 | 2022-02-06 19:53:10 +0100 | [diff] [blame] | 68 | static const struct dhcp_optflag d6_optflags[] ALIGN2 = { |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 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-Jonas | 23cbd7d | 2018-05-14 14:29:12 +1000 | [diff] [blame] | 80 | #if ENABLE_FEATURE_UDHCPC6_RFC5970 |
| 81 | { OPTION_STRING, D6_OPT_BOOT_URL }, |
| 82 | { OPTION_STRING, D6_OPT_BOOT_PARAM }, |
| 83 | #endif |
Samuel Mendoza-Jonas | 30f4d52 | 2018-05-14 14:29:13 +1000 | [diff] [blame] | 84 | { OPTION_STRING, 0xd1 }, /* DHCP_PXE_CONF_FILE */ |
| 85 | { OPTION_STRING, 0xd2 }, /* DHCP_PXE_PATH_PREFIX */ |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 86 | { 0, 0 } |
| 87 | }; |
| 88 | /* Must match d6_optflags[] order */ |
| 89 | static 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-Jonas | 23cbd7d | 2018-05-14 14:29:12 +1000 | [diff] [blame] | 101 | #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-Jonas | 30f4d52 | 2018-05-14 14:29:13 +1000 | [diff] [blame] | 105 | "pxeconffile" "\0" /* DHCP_PXE_CONF_FILE */ |
| 106 | "pxepathprefix" "\0" /* DHCP_PXE_PATH_PREFIX */ |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 107 | "\0"; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 108 | |
| 109 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 7e21f04 | 2011-11-08 11:39:41 +0100 | [diff] [blame] | 110 | static const char udhcpc6_longopts[] ALIGN1 = |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 111 | "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 Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 117 | "requestprefix\0" No_argument "d" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 118 | "script\0" Required_argument "s" |
| 119 | "timeout\0" Required_argument "T" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 120 | "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 Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 126 | "stateless\0" No_argument "l" |
Denys Vlasenko | ed820cc | 2017-05-08 15:11:02 +0200 | [diff] [blame] | 127 | USE_FOR_MMU( |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 128 | "background\0" No_argument "b" |
Denys Vlasenko | ed820cc | 2017-05-08 15:11:02 +0200 | [diff] [blame] | 129 | ) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 130 | /// 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 */ |
| 135 | enum { |
| 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 Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 151 | OPT_l = 1 << 15, |
| 152 | OPT_d = 1 << 16, |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 153 | /* The rest has variable bit positions, need to be clever */ |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 154 | OPTBIT_d = 16, |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 155 | 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 Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 163 | #if ENABLE_FEATURE_UDHCPC6_RFC4704 |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 164 | static const char opt_fqdn_req[] = { |
| 165 | (D6_OPT_CLIENT_FQDN >> 8), (D6_OPT_CLIENT_FQDN & 0xff), |
Denys Vlasenko | 470bebe | 2017-06-27 18:31:08 +0200 | [diff] [blame] | 166 | 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 Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 172 | }; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 173 | #endif |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 174 | |
| 175 | /*** Utility functions ***/ |
| 176 | |
| 177 | static 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 Vlasenko | 68c5b28 | 2011-11-07 16:21:24 +0100 | [diff] [blame] | 183 | if (option[3] > len_m4) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 184 | 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 Vlasenko | 68c5b28 | 2011-11-07 16:21:24 +0100 | [diff] [blame] | 189 | if (option[0] != 0 || option[2] != 0) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 190 | return NULL; |
| 191 | /* Option seems to be valid */ |
| 192 | /* Does its code match? */ |
Denys Vlasenko | 68c5b28 | 2011-11-07 16:21:24 +0100 | [diff] [blame] | 193 | if (option[1] == code) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 194 | return option; /* yes! */ |
Denys Vlasenko | 68c5b28 | 2011-11-07 16:21:24 +0100 | [diff] [blame] | 195 | len_m4 -= option[3] + 4; |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 196 | option += option[3] + 4; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static 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 Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 206 | return xmemdup(opt, opt[3] + 4); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 209 | /*** Script execution code ***/ |
| 210 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 211 | static char** new_env(void) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 212 | { |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 213 | 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 Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 217 | static char *string_option_to_env(const uint8_t *option, |
| 218 | const uint8_t *option_end) |
Samuel Mendoza-Jonas | bcdec1a | 2018-05-14 14:29:11 +1000 | [diff] [blame] | 219 | { |
| 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 Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 240 | bb_simple_error_msg("option data exceeds option length"); |
Samuel Mendoza-Jonas | bcdec1a | 2018-05-14 14:29:11 +1000 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | return xasprintf("%s=%.*s", name, val_len, (char*)option + 4); |
| 244 | } |
| 245 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 246 | /* put all the parameters into the environment */ |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 247 | static void option_to_env(const uint8_t *option, const uint8_t *option_end) |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 248 | { |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 249 | #if ENABLE_FEATURE_UDHCPC6_RFC3646 |
| 250 | int addrs, option_offset; |
| 251 | #endif |
Denys Vlasenko | ab03061 | 2017-03-27 22:49:12 +0200 | [diff] [blame] | 252 | /* "length minus 4" */ |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 253 | int len_m4 = option_end - option - 4; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 254 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 255 | 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 Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 262 | /* Check if option-length exceeds size of option */ |
| 263 | if (option[3] > len_m4) |
| 264 | break; |
| 265 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 266 | 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 Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 275 | /* 0 1 2 3 |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 276 | * 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 Decotigny | a174c79 | 2018-05-24 08:30:16 -0700 | [diff] [blame] | 290 | /* Make sure payload contains an address */ |
| 291 | if (option[3] < 24) |
| 292 | break; |
| 293 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 294 | sprint_nip6(ipv6str, option + 4); |
| 295 | *new_env() = xasprintf("ipv6=%s", ipv6str); |
| 296 | |
| 297 | move_from_unaligned32(v32, option + 4 + 16 + 4); |
Denys Vlasenko | 9b6eb2a | 2022-07-13 16:15:04 +0200 | [diff] [blame] | 298 | v32 = ntohl(v32); |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 299 | *new_env() = xasprintf("lease=%u", (unsigned)v32); |
| 300 | break; |
| 301 | |
| 302 | //case D6_OPT_ORO: |
| 303 | //case D6_OPT_PREFERENCE: |
| 304 | //case D6_OPT_ELAPSED_TIME: |
| 305 | //case D6_OPT_RELAY_MSG: |
| 306 | //case D6_OPT_AUTH: |
| 307 | //case D6_OPT_UNICAST: |
| 308 | //case D6_OPT_STATUS_CODE: |
| 309 | //case D6_OPT_RAPID_COMMIT: |
| 310 | //case D6_OPT_USER_CLASS: |
| 311 | //case D6_OPT_VENDOR_CLASS: |
| 312 | //case D6_OPT_VENDOR_OPTS: |
| 313 | //case D6_OPT_INTERFACE_ID: |
| 314 | //case D6_OPT_RECONF_MSG: |
| 315 | //case D6_OPT_RECONF_ACCEPT: |
| 316 | |
| 317 | case D6_OPT_IAPREFIX: |
| 318 | /* 0 1 2 3 |
| 319 | * 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 |
| 320 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 321 | * | OPTION_IAPREFIX | option-length | |
| 322 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 323 | * | preferred-lifetime | |
| 324 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 325 | * | valid-lifetime | |
| 326 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 327 | * | prefix-length | | |
| 328 | * +-+-+-+-+-+-+-+-+ IPv6 prefix | |
| 329 | * | (16 octets) | |
| 330 | * | | |
| 331 | * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 332 | * | | |
| 333 | * +-+-+-+-+-+-+-+-+ |
| 334 | */ |
Denys Vlasenko | 688cb3b | 2018-01-16 16:00:13 +0100 | [diff] [blame] | 335 | move_from_unaligned32(v32, option + 4 + 4); |
Denys Vlasenko | 9b6eb2a | 2022-07-13 16:15:04 +0200 | [diff] [blame] | 336 | v32 = ntohl(v32); |
Denys Vlasenko | 688cb3b | 2018-01-16 16:00:13 +0100 | [diff] [blame] | 337 | *new_env() = xasprintf("ipv6prefix_lease=%u", (unsigned)v32); |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 338 | |
Denys Vlasenko | 688cb3b | 2018-01-16 16:00:13 +0100 | [diff] [blame] | 339 | sprint_nip6(ipv6str, option + 4 + 4 + 4 + 1); |
| 340 | *new_env() = xasprintf("ipv6prefix=%s/%u", ipv6str, (unsigned)(option[4 + 4 + 4])); |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 341 | break; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 342 | #if ENABLE_FEATURE_UDHCPC6_RFC3646 |
| 343 | case D6_OPT_DNS_SERVERS: { |
| 344 | char *dlist; |
| 345 | |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 346 | /* Make sure payload-size is a multiple of 16 */ |
| 347 | if ((option[3] & 0x0f) != 0) |
| 348 | break; |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 349 | |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 350 | /* Get the number of addresses on the option */ |
| 351 | addrs = option[3] >> 4; |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 352 | |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 353 | /* Setup environment variable */ |
| 354 | *new_env() = dlist = xmalloc(4 + addrs * 40 - 1); |
| 355 | dlist = stpcpy(dlist, "dns="); |
| 356 | option_offset = 0; |
| 357 | |
| 358 | while (addrs--) { |
| 359 | sprint_nip6(dlist, option + 4 + option_offset); |
| 360 | dlist += 39; |
| 361 | option_offset += 16; |
| 362 | if (addrs) |
| 363 | *dlist++ = ' '; |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 366 | break; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 367 | } |
| 368 | case D6_OPT_DOMAIN_LIST: { |
| 369 | char *dlist; |
| 370 | |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 371 | dlist = dname_dec(option + 4, (option[2] << 8) | option[3], "search="); |
| 372 | if (!dlist) |
| 373 | break; |
| 374 | *new_env() = dlist; |
| 375 | break; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 376 | } |
| 377 | #endif |
| 378 | #if ENABLE_FEATURE_UDHCPC6_RFC4704 |
| 379 | case D6_OPT_CLIENT_FQDN: { |
| 380 | char *dlist; |
| 381 | |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 382 | if (option[3] == 0) |
| 383 | break; |
| 384 | /* Work around broken ISC DHCPD6. |
| 385 | * ISC DHCPD6 does not implement RFC 4704 correctly: It says the first |
| 386 | * byte of option-payload should contain flags where the bits 7-3 are |
| 387 | * reserved for future use and MUST be zero. Instead ISC DHCPD6 just |
| 388 | * writes the entire FQDN as string to option-payload. We assume a |
| 389 | * broken server here if any of the reserved bits are set. |
| 390 | */ |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 391 | if (option[4] & 0xf8) { |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 392 | *new_env() = xasprintf("fqdn=%.*s", (int)option[3], (char*)option + 4); |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 393 | break; |
| 394 | } |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 395 | dlist = dname_dec(option + 5, (/*(option[2] << 8) |*/ option[3]) - 1, "fqdn="); |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 396 | if (!dlist) |
| 397 | break; |
| 398 | *new_env() = dlist; |
| 399 | break; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 400 | } |
| 401 | #endif |
| 402 | #if ENABLE_FEATURE_UDHCPC6_RFC4833 |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 403 | /* RFC 4833 Timezones */ |
| 404 | case D6_OPT_TZ_POSIX: |
| 405 | *new_env() = xasprintf("tz=%.*s", (int)option[3], (char*)option + 4); |
| 406 | break; |
| 407 | case D6_OPT_TZ_NAME: |
| 408 | *new_env() = xasprintf("tz_name=%.*s", (int)option[3], (char*)option + 4); |
| 409 | break; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 410 | #endif |
Samuel Mendoza-Jonas | 23cbd7d | 2018-05-14 14:29:12 +1000 | [diff] [blame] | 411 | case D6_OPT_BOOT_URL: |
| 412 | case D6_OPT_BOOT_PARAM: |
Samuel Mendoza-Jonas | 30f4d52 | 2018-05-14 14:29:13 +1000 | [diff] [blame] | 413 | case 0xd1: /* DHCP_PXE_CONF_FILE */ |
| 414 | case 0xd2: /* DHCP_PXE_PATH_PREFIX */ |
Samuel Mendoza-Jonas | 23cbd7d | 2018-05-14 14:29:12 +1000 | [diff] [blame] | 415 | { |
| 416 | char *tmp = string_option_to_env(option, option_end); |
| 417 | if (tmp) |
| 418 | *new_env() = tmp; |
| 419 | break; |
| 420 | } |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 421 | } |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 422 | len_m4 -= 4 + option[3]; |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 423 | option += 4 + option[3]; |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 427 | static char **fill_envp(const uint8_t *option, const uint8_t *option_end) |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 428 | { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 429 | char **envp, **curr; |
| 430 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 431 | client6_data.env_ptr = NULL; |
| 432 | client6_data.env_idx = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 433 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 434 | *new_env() = xasprintf("interface=%s", client_data.interface); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 435 | |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 436 | if (option) |
| 437 | option_to_env(option, option_end); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 438 | |
Denys Vlasenko | a092a89 | 2011-11-16 20:17:12 +0100 | [diff] [blame] | 439 | envp = curr = client6_data.env_ptr; |
| 440 | while (*curr) |
| 441 | putenv(*curr++); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 442 | |
| 443 | return envp; |
| 444 | } |
| 445 | |
Denys Vlasenko | f6def87 | 2021-06-03 16:14:04 +0200 | [diff] [blame] | 446 | /* Call a script with env vars */ |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 447 | static void d6_run_script(const uint8_t *option, const uint8_t *option_end, |
| 448 | const char *name) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 449 | { |
| 450 | char **envp, **curr; |
| 451 | char *argv[3]; |
| 452 | |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 453 | envp = fill_envp(option, option_end); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 454 | |
| 455 | /* call script */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 456 | log1("executing %s %s", client_data.script, name); |
| 457 | argv[0] = (char*) client_data.script; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 458 | argv[1] = (char*) name; |
| 459 | argv[2] = NULL; |
| 460 | spawn_and_wait(argv); |
| 461 | |
| 462 | for (curr = envp; *curr; curr++) { |
| 463 | log2(" %s", *curr); |
| 464 | bb_unsetenv_and_free(*curr); |
| 465 | } |
| 466 | free(envp); |
| 467 | } |
| 468 | |
Denys Vlasenko | f6def87 | 2021-06-03 16:14:04 +0200 | [diff] [blame] | 469 | /* Call a script with no env var */ |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 470 | static void d6_run_script_no_option(const char *name) |
| 471 | { |
| 472 | d6_run_script(NULL, NULL, name); |
| 473 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 474 | |
| 475 | /*** Sending/receiving packets ***/ |
| 476 | |
| 477 | static ALWAYS_INLINE uint32_t random_xid(void) |
| 478 | { |
| 479 | uint32_t t = rand() & htonl(0x00ffffff); |
| 480 | return t; |
| 481 | } |
| 482 | |
| 483 | /* Initialize the packet with the proper defaults */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 484 | static uint8_t *init_d6_packet(struct d6_packet *packet, char type) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 485 | { |
Denys Vlasenko | 11e024a | 2019-09-24 14:01:00 +0200 | [diff] [blame] | 486 | uint8_t *ptr; |
Denys Vlasenko | 11e024a | 2019-09-24 14:01:00 +0200 | [diff] [blame] | 487 | unsigned secs; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 488 | |
| 489 | memset(packet, 0, sizeof(*packet)); |
| 490 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 491 | packet->d6_xid32 = client_data.xid; |
| 492 | packet->d6_msg_type = type; /* union, overwrites lowest byte of d6_xid32 */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 493 | |
Denys Vlasenko | 11e024a | 2019-09-24 14:01:00 +0200 | [diff] [blame] | 494 | /* ELAPSED_TIME option is required to be present by the RFC, |
| 495 | * and some servers do check for its presense. [which?] |
| 496 | */ |
| 497 | ptr = packet->d6_options; /* NB: it is 32-bit aligned */ |
| 498 | *((uint32_t*)ptr) = htonl((D6_OPT_ELAPSED_TIME << 16) + 2); |
| 499 | ptr += 4; |
| 500 | client_data.last_secs = monotonic_sec(); |
| 501 | if (client_data.first_secs == 0) |
| 502 | client_data.first_secs = client_data.last_secs; |
| 503 | secs = client_data.last_secs - client_data.first_secs; |
| 504 | *((uint16_t*)ptr) = (secs < 0xffff) ? htons(secs) : 0xffff; |
| 505 | ptr += 2; |
| 506 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 507 | return ptr; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static uint8_t *add_d6_client_options(uint8_t *ptr) |
| 511 | { |
Denys Vlasenko | 6027597 | 2018-05-14 11:06:35 +0200 | [diff] [blame] | 512 | struct option_set *curr; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 513 | uint8_t *start = ptr; |
| 514 | unsigned option; |
Denys Vlasenko | 6027597 | 2018-05-14 11:06:35 +0200 | [diff] [blame] | 515 | uint16_t len; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 516 | |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 517 | ptr += 4; |
| 518 | for (option = 1; option < 256; option++) { |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 519 | if (client_data.opt_mask[option >> 3] & (1 << (option & 7))) { |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 520 | ptr[0] = (option >> 8); |
| 521 | ptr[1] = option; |
| 522 | ptr += 2; |
| 523 | } |
| 524 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 525 | |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 526 | if ((ptr - start - 4) != 0) { |
| 527 | start[0] = (D6_OPT_ORO >> 8); |
| 528 | start[1] = D6_OPT_ORO; |
| 529 | start[2] = ((ptr - start - 4) >> 8); |
| 530 | start[3] = (ptr - start - 4); |
| 531 | } else |
| 532 | ptr = start; |
| 533 | |
| 534 | #if ENABLE_FEATURE_UDHCPC6_RFC4704 |
| 535 | ptr = mempcpy(ptr, &opt_fqdn_req, sizeof(opt_fqdn_req)); |
| 536 | #endif |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 537 | /* Add -x options if any */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 538 | curr = client_data.options; |
Denys Vlasenko | 6027597 | 2018-05-14 11:06:35 +0200 | [diff] [blame] | 539 | while (curr) { |
| 540 | len = (curr->data[D6_OPT_LEN] << 8) | curr->data[D6_OPT_LEN + 1]; |
| 541 | ptr = mempcpy(ptr, curr->data, D6_OPT_DATA + len); |
| 542 | curr = curr->next; |
| 543 | } |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 544 | |
| 545 | return ptr; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 548 | static int d6_mcast_from_client_data_ifindex(struct d6_packet *packet, uint8_t *end) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 549 | { |
Denys Vlasenko | 0d75e8b | 2019-04-13 19:43:15 +0200 | [diff] [blame] | 550 | /* FF02::1:2 is "All_DHCP_Relay_Agents_and_Servers" address */ |
Denys Vlasenko | 301ef96 | 2022-12-15 11:49:00 +0100 | [diff] [blame] | 551 | static const uint8_t FF02__1_2[16] ALIGNED(sizeof(long)) = { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 552 | 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 553 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, |
| 554 | }; |
Uwe Glaeser | faab906 | 2020-06-21 02:56:12 +0200 | [diff] [blame] | 555 | /* IPv6 requires different multicast contents in Ethernet Frame (RFC 2464) */ |
| 556 | static const uint8_t MAC_DHCP6MCAST_ADDR[6] ALIGN2 = { |
| 557 | 0x33, 0x33, 0x00, 0x01, 0x00, 0x02, |
| 558 | }; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 559 | |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 560 | return d6_send_raw_packet_from_client_data_ifindex( |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 561 | packet, (end - (uint8_t*) packet), |
Denys Vlasenko | e09f5e3 | 2017-03-27 22:10:15 +0200 | [diff] [blame] | 562 | /*src*/ &client6_data.ll_ip6, CLIENT_PORT6, |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 563 | /*dst*/ (struct in6_addr*)FF02__1_2, SERVER_PORT6, MAC_DHCP6MCAST_ADDR |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 564 | ); |
| 565 | } |
| 566 | |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 567 | /* RFC 3315 18.1.5. Creation and Transmission of Information-request Messages |
| 568 | * |
| 569 | * The client uses an Information-request message to obtain |
| 570 | * configuration information without having addresses assigned to it. |
| 571 | * |
| 572 | * The client sets the "msg-type" field to INFORMATION-REQUEST. The |
| 573 | * client generates a transaction ID and inserts this value in the |
| 574 | * "transaction-id" field. |
| 575 | * |
| 576 | * The client SHOULD include a Client Identifier option to identify |
| 577 | * itself to the server. If the client does not include a Client |
| 578 | * Identifier option, the server will not be able to return any client- |
| 579 | * specific options to the client, or the server may choose not to |
| 580 | * respond to the message at all. The client MUST include a Client |
| 581 | * Identifier option if the Information-Request message will be |
| 582 | * authenticated. |
| 583 | * |
| 584 | * The client MUST include an Option Request option (see section 22.7) |
| 585 | * to indicate the options the client is interested in receiving. The |
| 586 | * client MAY include options with data values as hints to the server |
| 587 | * about parameter values the client would like to have returned. |
| 588 | */ |
| 589 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 590 | static NOINLINE int send_d6_info_request(void) |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 591 | { |
| 592 | struct d6_packet packet; |
| 593 | uint8_t *opt_ptr; |
| 594 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 595 | /* Fill in: msg type, xid, ELAPSED_TIME */ |
| 596 | opt_ptr = init_d6_packet(&packet, D6_MSG_INFORMATION_REQUEST); |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 597 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 598 | /* Add options: client-id, |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 599 | * "param req" option according to -O, options specified with -x |
| 600 | */ |
| 601 | opt_ptr = add_d6_client_options(opt_ptr); |
| 602 | |
| 603 | bb_error_msg("sending %s", "info request"); |
| 604 | return d6_mcast_from_client_data_ifindex(&packet, opt_ptr); |
| 605 | } |
| 606 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 607 | /* Milticast a DHCPv6 Solicit packet to the network, with an optionally requested IP. |
| 608 | * |
| 609 | * RFC 3315 17.1.1. Creation of Solicit Messages |
| 610 | * |
| 611 | * The client MUST include a Client Identifier option to identify itself |
| 612 | * to the server. The client includes IA options for any IAs to which |
| 613 | * it wants the server to assign addresses. The client MAY include |
| 614 | * addresses in the IAs as a hint to the server about addresses for |
| 615 | * which the client has a preference. ... |
| 616 | * |
| 617 | * The client uses IA_NA options to request the assignment of non- |
| 618 | * temporary addresses and uses IA_TA options to request the assignment |
| 619 | * of temporary addresses. Either IA_NA or IA_TA options, or a |
| 620 | * combination of both, can be included in DHCP messages. |
| 621 | * |
| 622 | * The client SHOULD include an Option Request option (see section 22.7) |
| 623 | * to indicate the options the client is interested in receiving. The |
| 624 | * client MAY additionally include instances of those options that are |
| 625 | * identified in the Option Request option, with data values as hints to |
| 626 | * the server about parameter values the client would like to have |
| 627 | * returned. |
| 628 | * |
| 629 | * The client includes a Reconfigure Accept option (see section 22.20) |
| 630 | * if the client is willing to accept Reconfigure messages from the |
| 631 | * server. |
| 632 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 633 | | OPTION_CLIENTID | option-len | |
| 634 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 635 | . . |
| 636 | . DUID . |
| 637 | . (variable length) . |
| 638 | . . |
| 639 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 640 | |
| 641 | |
| 642 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 643 | | OPTION_IA_NA | option-len | |
| 644 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 645 | | IAID (4 octets) | |
| 646 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 647 | | T1 | |
| 648 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 649 | | T2 | |
| 650 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 651 | | | |
| 652 | . IA_NA-options . |
| 653 | . . |
| 654 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 655 | |
| 656 | |
| 657 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 658 | | OPTION_IAADDR | option-len | |
| 659 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 660 | | | |
| 661 | | IPv6 address | |
| 662 | | | |
| 663 | | | |
| 664 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 665 | | preferred-lifetime | |
| 666 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 667 | | valid-lifetime | |
| 668 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 669 | . . |
| 670 | . IAaddr-options . |
| 671 | . . |
| 672 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 673 | |
| 674 | |
| 675 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 676 | | OPTION_ORO | option-len | |
| 677 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 678 | | requested-option-code-1 | requested-option-code-2 | |
| 679 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 680 | | ... | |
| 681 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 682 | |
| 683 | |
| 684 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 685 | | OPTION_RECONF_ACCEPT | 0 | |
| 686 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 687 | */ |
| 688 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 689 | static NOINLINE int send_d6_discover(struct in6_addr *requested_ipv6) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 690 | { |
| 691 | struct d6_packet packet; |
| 692 | uint8_t *opt_ptr; |
| 693 | unsigned len; |
| 694 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 695 | /* Fill in: msg type, xid, ELAPSED_TIME */ |
| 696 | opt_ptr = init_d6_packet(&packet, D6_MSG_SOLICIT); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 697 | |
| 698 | /* Create new IA_NA, optionally with included IAADDR with requested IP */ |
| 699 | free(client6_data.ia_na); |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 700 | client6_data.ia_na = NULL; |
| 701 | if (option_mask32 & OPT_r) { |
| 702 | len = requested_ipv6 ? 2+2+4+4+4 + 2+2+16+4+4 : 2+2+4+4+4; |
| 703 | client6_data.ia_na = xzalloc(len); |
| 704 | client6_data.ia_na->code = D6_OPT_IA_NA; |
| 705 | client6_data.ia_na->len = len - 4; |
Denys Vlasenko | 72f12ac | 2019-06-11 18:18:48 +0200 | [diff] [blame] | 706 | *(bb__aliased_uint32_t*)client6_data.ia_na->data = rand(); /* IAID */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 707 | if (requested_ipv6) { |
| 708 | struct d6_option *iaaddr = (void*)(client6_data.ia_na->data + 4+4+4); |
| 709 | iaaddr->code = D6_OPT_IAADDR; |
| 710 | iaaddr->len = 16+4+4; |
| 711 | memcpy(iaaddr->data, requested_ipv6, 16); |
| 712 | } |
| 713 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, len); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 714 | } |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 715 | |
| 716 | /* IA_PD */ |
| 717 | free(client6_data.ia_pd); |
| 718 | client6_data.ia_pd = NULL; |
| 719 | if (option_mask32 & OPT_d) { |
| 720 | len = 2+2+4+4+4; |
| 721 | client6_data.ia_pd = xzalloc(len); |
| 722 | client6_data.ia_pd->code = D6_OPT_IA_PD; |
| 723 | client6_data.ia_pd->len = len - 4; |
Denys Vlasenko | 72f12ac | 2019-06-11 18:18:48 +0200 | [diff] [blame] | 724 | *(bb__aliased_uint32_t*)client6_data.ia_pd->data = rand(); /* IAID */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 725 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, len); |
| 726 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 727 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 728 | /* Add options: client-id, |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 729 | * "param req" option according to -O, options specified with -x |
| 730 | */ |
| 731 | opt_ptr = add_d6_client_options(opt_ptr); |
| 732 | |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 733 | bb_info_msg("sending %s", "discover"); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 734 | return d6_mcast_from_client_data_ifindex(&packet, opt_ptr); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* Multicast a DHCPv6 request message |
| 738 | * |
| 739 | * RFC 3315 18.1.1. Creation and Transmission of Request Messages |
| 740 | * |
| 741 | * The client uses a Request message to populate IAs with addresses and |
| 742 | * obtain other configuration information. The client includes one or |
| 743 | * more IA options in the Request message. The server then returns |
| 744 | * addresses and other information about the IAs to the client in IA |
| 745 | * options in a Reply message. |
| 746 | * |
| 747 | * The client generates a transaction ID and inserts this value in the |
| 748 | * "transaction-id" field. |
| 749 | * |
| 750 | * The client places the identifier of the destination server in a |
| 751 | * Server Identifier option. |
| 752 | * |
| 753 | * The client MUST include a Client Identifier option to identify itself |
| 754 | * to the server. The client adds any other appropriate options, |
| 755 | * including one or more IA options (if the client is requesting that |
| 756 | * the server assign it some network addresses). |
| 757 | * |
| 758 | * The client MUST include an Option Request option (see section 22.7) |
| 759 | * to indicate the options the client is interested in receiving. The |
| 760 | * client MAY include options with data values as hints to the server |
| 761 | * about parameter values the client would like to have returned. |
| 762 | * |
| 763 | * The client includes a Reconfigure Accept option (see section 22.20) |
| 764 | * indicating whether or not the client is willing to accept Reconfigure |
| 765 | * messages from the server. |
| 766 | */ |
| 767 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 768 | static NOINLINE int send_d6_select(void) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 769 | { |
| 770 | struct d6_packet packet; |
| 771 | uint8_t *opt_ptr; |
| 772 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 773 | /* Fill in: msg type, xid, ELAPSED_TIME */ |
| 774 | opt_ptr = init_d6_packet(&packet, D6_MSG_REQUEST); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 775 | |
| 776 | /* server id */ |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 777 | opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 778 | /* IA NA (contains requested IP) */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 779 | if (client6_data.ia_na) |
| 780 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2); |
| 781 | /* IA PD */ |
| 782 | if (client6_data.ia_pd) |
| 783 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 784 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 785 | /* Add options: client-id, |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 786 | * "param req" option according to -O, options specified with -x |
| 787 | */ |
| 788 | opt_ptr = add_d6_client_options(opt_ptr); |
| 789 | |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 790 | bb_info_msg("sending %s", "select"); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 791 | return d6_mcast_from_client_data_ifindex(&packet, opt_ptr); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Unicast or broadcast a DHCP renew message |
| 795 | * |
| 796 | * RFC 3315 18.1.3. Creation and Transmission of Renew Messages |
| 797 | * |
| 798 | * To extend the valid and preferred lifetimes for the addresses |
| 799 | * associated with an IA, the client sends a Renew message to the server |
| 800 | * from which the client obtained the addresses in the IA containing an |
| 801 | * IA option for the IA. The client includes IA Address options in the |
| 802 | * IA option for the addresses associated with the IA. The server |
| 803 | * determines new lifetimes for the addresses in the IA according to the |
| 804 | * administrative configuration of the server. The server may also add |
| 805 | * new addresses to the IA. The server may remove addresses from the IA |
| 806 | * by setting the preferred and valid lifetimes of those addresses to |
| 807 | * zero. |
| 808 | * |
| 809 | * The server controls the time at which the client contacts the server |
| 810 | * to extend the lifetimes on assigned addresses through the T1 and T2 |
| 811 | * parameters assigned to an IA. |
| 812 | * |
| 813 | * At time T1 for an IA, the client initiates a Renew/Reply message |
| 814 | * exchange to extend the lifetimes on any addresses in the IA. The |
| 815 | * client includes an IA option with all addresses currently assigned to |
| 816 | * the IA in its Renew message. |
| 817 | * |
| 818 | * If T1 or T2 is set to 0 by the server (for an IA_NA) or there are no |
| 819 | * T1 or T2 times (for an IA_TA), the client may send a Renew or Rebind |
| 820 | * message, respectively, at the client's discretion. |
| 821 | * |
| 822 | * The client sets the "msg-type" field to RENEW. The client generates |
| 823 | * a transaction ID and inserts this value in the "transaction-id" |
| 824 | * field. |
| 825 | * |
| 826 | * The client places the identifier of the destination server in a |
| 827 | * Server Identifier option. |
| 828 | * |
| 829 | * The client MUST include a Client Identifier option to identify itself |
| 830 | * to the server. The client adds any appropriate options, including |
| 831 | * one or more IA options. The client MUST include the list of |
| 832 | * addresses the client currently has associated with the IAs in the |
| 833 | * Renew message. |
| 834 | * |
| 835 | * The client MUST include an Option Request option (see section 22.7) |
| 836 | * to indicate the options the client is interested in receiving. The |
| 837 | * client MAY include options with data values as hints to the server |
| 838 | * about parameter values the client would like to have returned. |
| 839 | */ |
| 840 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 841 | static NOINLINE int send_d6_renew(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 842 | { |
| 843 | struct d6_packet packet; |
| 844 | uint8_t *opt_ptr; |
| 845 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 846 | /* Fill in: msg type, xid, ELAPSED_TIME */ |
Denys Vlasenko | 75aaa8b | 2022-08-02 15:33:51 +0200 | [diff] [blame] | 847 | opt_ptr = init_d6_packet(&packet, D6_MSG_RENEW); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 848 | |
| 849 | /* server id */ |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 850 | opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 851 | /* IA NA (contains requested IP) */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 852 | if (client6_data.ia_na) |
| 853 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2); |
| 854 | /* IA PD */ |
| 855 | if (client6_data.ia_pd) |
| 856 | opt_ptr = mempcpy(opt_ptr, client6_data.ia_pd, client6_data.ia_pd->len + 2+2); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 857 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 858 | /* Add options: client-id, |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 859 | * "param req" option according to -O, options specified with -x |
| 860 | */ |
| 861 | opt_ptr = add_d6_client_options(opt_ptr); |
| 862 | |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 863 | bb_info_msg("sending %s", "renew"); |
Denys Vlasenko | 148788e | 2018-06-21 17:36:22 +0200 | [diff] [blame] | 864 | if (server_ipv6) |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 865 | return d6_send_kernel_packet_from_client_data_ifindex( |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 866 | &packet, (opt_ptr - (uint8_t*) &packet), |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 867 | our_cur_ipv6, CLIENT_PORT6, |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 868 | server_ipv6, SERVER_PORT6 |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 869 | ); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 870 | return d6_mcast_from_client_data_ifindex(&packet, opt_ptr); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* Unicast a DHCP release message */ |
Denys Vlasenko | 9d05ad0 | 2018-11-03 23:34:03 +0100 | [diff] [blame] | 874 | static |
| 875 | ALWAYS_INLINE /* one caller, help compiler to use this fact */ |
| 876 | int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 877 | { |
| 878 | struct d6_packet packet; |
| 879 | uint8_t *opt_ptr; |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 880 | struct option_set *ci; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 881 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 882 | /* Fill in: msg type, xid, ELAPSED_TIME */ |
| 883 | opt_ptr = init_d6_packet(&packet, D6_MSG_RELEASE); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 884 | /* server id */ |
Denys Vlasenko | 234b82c | 2017-06-26 19:42:48 +0200 | [diff] [blame] | 885 | opt_ptr = mempcpy(opt_ptr, client6_data.server_id, client6_data.server_id->len + 2+2); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 886 | /* IA NA (contains our current IP) */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 887 | 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 Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 892 | /* Client-id */ |
Denys Vlasenko | e67b80f | 2021-12-12 17:13:54 +0100 | [diff] [blame] | 893 | ci = udhcp_find_option(client_data.options, D6_OPT_CLIENTID, /*dhcpv6:*/ 1); |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 894 | if (ci) |
| 895 | opt_ptr = mempcpy(opt_ptr, ci->data, D6_OPT_DATA + 2+2 + 6); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 896 | |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 897 | bb_info_msg("sending %s", "release"); |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 898 | return d6_send_kernel_packet_from_client_data_ifindex( |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 899 | &packet, (opt_ptr - (uint8_t*) &packet), |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 900 | our_cur_ipv6, CLIENT_PORT6, |
Denys Vlasenko | f3d6711 | 2020-12-15 21:55:15 +0100 | [diff] [blame] | 901 | server_ipv6, SERVER_PORT6 |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 902 | ); |
| 903 | } |
| 904 | |
| 905 | /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ |
| 906 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | ed898ed | 2017-03-27 22:32:44 +0200 | [diff] [blame] | 907 | static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6, struct d6_packet *d6_pkt, int fd) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 908 | { |
| 909 | int bytes; |
| 910 | struct ip6_udp_d6_packet packet; |
| 911 | |
| 912 | bytes = safe_read(fd, &packet, sizeof(packet)); |
| 913 | if (bytes < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 914 | log1s("packet read error, ignoring"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 915 | /* NB: possible down interface, etc. Caller should pause. */ |
| 916 | return bytes; /* returns -1 */ |
| 917 | } |
| 918 | |
| 919 | if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 920 | log1s("packet is too short, ignoring"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 921 | return -2; |
| 922 | } |
| 923 | |
| 924 | if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) { |
| 925 | /* packet is bigger than sizeof(packet), we did partial read */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 926 | log1s("oversized packet, ignoring"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 927 | return -2; |
| 928 | } |
| 929 | |
| 930 | /* ignore any extra garbage bytes */ |
| 931 | bytes = sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen); |
| 932 | |
| 933 | /* make sure its the right packet for us, and that it passes sanity checks */ |
| 934 | if (packet.ip6.ip6_nxt != IPPROTO_UDP |
| 935 | || (packet.ip6.ip6_vfc >> 4) != 6 |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 936 | || packet.udp.dest != htons(CLIENT_PORT6) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 937 | /* || bytes > (int) sizeof(packet) - can't happen */ |
| 938 | || packet.udp.len != packet.ip6.ip6_plen |
| 939 | ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 940 | log1s("unrelated/bogus packet, ignoring"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 941 | return -2; |
| 942 | } |
| 943 | |
| 944 | //How to do this for ipv6? |
| 945 | // /* verify UDP checksum. IP header has to be modified for this */ |
| 946 | // memset(&packet.ip, 0, offsetof(struct iphdr, protocol)); |
| 947 | // /* ip.xx fields which are not memset: protocol, check, saddr, daddr */ |
| 948 | // packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ |
| 949 | // check = packet.udp.check; |
| 950 | // packet.udp.check = 0; |
Denys Vlasenko | 4a0eb03 | 2020-10-01 03:07:22 +0200 | [diff] [blame] | 951 | // if (check && check != inet_cksum(&packet, bytes)) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 952 | // log1("packet with bad UDP checksum received, ignoring"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 953 | // return -2; |
| 954 | // } |
| 955 | |
Denys Vlasenko | ed898ed | 2017-03-27 22:32:44 +0200 | [diff] [blame] | 956 | if (peer_ipv6) |
| 957 | *peer_ipv6 = packet.ip6.ip6_src; /* struct copy */ |
| 958 | |
Denys Vlasenko | 4bbc391 | 2021-06-02 19:51:52 +0200 | [diff] [blame] | 959 | log2("received %s", "a packet"); |
| 960 | /* log2 because more informative msg for valid packets is printed later at log1 level */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 961 | d6_dump_packet(&packet.data); |
| 962 | |
| 963 | bytes -= sizeof(packet.ip6) + sizeof(packet.udp); |
| 964 | memcpy(d6_pkt, &packet.data, bytes); |
| 965 | return bytes; |
| 966 | } |
| 967 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 968 | /*** Main ***/ |
| 969 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 970 | /* Values for client_data.listen_mode */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 971 | #define LISTEN_NONE 0 |
| 972 | #define LISTEN_KERNEL 1 |
| 973 | #define LISTEN_RAW 2 |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 974 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 975 | /* Values for client_data.state */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 976 | /* initial state: (re)start DHCP negotiation */ |
| 977 | #define INIT_SELECTING 0 |
| 978 | /* discover was sent, DHCPOFFER reply received */ |
| 979 | #define REQUESTING 1 |
| 980 | /* select/renew was sent, DHCPACK reply received */ |
| 981 | #define BOUND 2 |
| 982 | /* half of lease passed, want to renew it by sending unicast renew requests */ |
| 983 | #define RENEWING 3 |
| 984 | /* renew requests were not answered, lease is almost over, send broadcast renew */ |
| 985 | #define REBINDING 4 |
| 986 | /* manually requested renew (SIGUSR1) */ |
| 987 | #define RENEW_REQUESTED 5 |
| 988 | /* release, possibly manually requested (SIGUSR2) */ |
| 989 | #define RELEASED 6 |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 990 | |
| 991 | static int d6_raw_socket(int ifindex) |
| 992 | { |
| 993 | int fd; |
| 994 | struct sockaddr_ll sock; |
| 995 | |
| 996 | /* |
| 997 | * Comment: |
| 998 | * |
| 999 | * I've selected not to see LL header, so BPF doesn't see it, too. |
| 1000 | * The filter may also pass non-IP and non-ARP packets, but we do |
| 1001 | * a more complete check when receiving the message in userspace. |
| 1002 | * |
| 1003 | * and filter shamelessly stolen from: |
| 1004 | * |
| 1005 | * http://www.flamewarmaster.de/software/dhcpclient/ |
| 1006 | * |
| 1007 | * There are a few other interesting ideas on that page (look under |
| 1008 | * "Motivation"). Use of netlink events is most interesting. Think |
| 1009 | * of various network servers listening for events and reconfiguring. |
| 1010 | * That would obsolete sending HUP signals and/or make use of restarts. |
| 1011 | * |
| 1012 | * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>. |
| 1013 | * License: GPL v2. |
| 1014 | * |
| 1015 | * TODO: make conditional? |
| 1016 | */ |
| 1017 | #if 0 |
| 1018 | static const struct sock_filter filter_instr[] = { |
| 1019 | /* load 9th byte (protocol) */ |
| 1020 | BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9), |
| 1021 | /* jump to L1 if it is IPPROTO_UDP, else to L4 */ |
| 1022 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6), |
| 1023 | /* L1: load halfword from offset 6 (flags and frag offset) */ |
| 1024 | BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6), |
| 1025 | /* jump to L4 if any bits in frag offset field are set, else to L2 */ |
| 1026 | BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0), |
| 1027 | /* L2: skip IP header (load index reg with header len) */ |
| 1028 | BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), |
| 1029 | /* load udp destination port from halfword[header_len + 2] */ |
| 1030 | BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2), |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 1031 | /* jump to L3 if udp dport is CLIENT_PORT6, else to L4 */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1032 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1), |
| 1033 | /* L3: accept packet */ |
Denys Vlasenko | ffc3a93 | 2014-02-19 14:17:11 +0100 | [diff] [blame] | 1034 | BPF_STMT(BPF_RET|BPF_K, 0x7fffffff), |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1035 | /* L4: discard packet */ |
| 1036 | BPF_STMT(BPF_RET|BPF_K, 0), |
| 1037 | }; |
| 1038 | static const struct sock_fprog filter_prog = { |
| 1039 | .len = sizeof(filter_instr) / sizeof(filter_instr[0]), |
| 1040 | /* casting const away: */ |
| 1041 | .filter = (struct sock_filter *) filter_instr, |
| 1042 | }; |
| 1043 | #endif |
| 1044 | |
Denys Vlasenko | 168f0ef | 2017-07-21 12:04:22 +0200 | [diff] [blame] | 1045 | log2("opening raw socket on ifindex %d", ifindex); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1046 | |
| 1047 | fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6)); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1048 | |
Denys Vlasenko | 2b9acc6 | 2017-09-29 14:09:02 +0200 | [diff] [blame] | 1049 | memset(&sock, 0, sizeof(sock)); /* let's be deterministic */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1050 | sock.sll_family = AF_PACKET; |
| 1051 | sock.sll_protocol = htons(ETH_P_IPV6); |
| 1052 | sock.sll_ifindex = ifindex; |
Denys Vlasenko | 2b9acc6 | 2017-09-29 14:09:02 +0200 | [diff] [blame] | 1053 | /*sock.sll_hatype = ARPHRD_???;*/ |
| 1054 | /*sock.sll_pkttype = PACKET_???;*/ |
| 1055 | /*sock.sll_halen = ???;*/ |
| 1056 | /*sock.sll_addr[8] = ???;*/ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1057 | xbind(fd, (struct sockaddr *) &sock, sizeof(sock)); |
| 1058 | |
| 1059 | #if 0 |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 1060 | if (CLIENT_PORT6 == 546) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1061 | /* Use only if standard port is in use */ |
| 1062 | /* Ignoring error (kernel may lack support for this) */ |
| 1063 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, |
| 1064 | sizeof(filter_prog)) >= 0) |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1065 | log1("attached filter to raw socket fd %d", fd); // log? |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1066 | } |
| 1067 | #endif |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1068 | return fd; |
| 1069 | } |
| 1070 | |
| 1071 | static void change_listen_mode(int new_mode) |
| 1072 | { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1073 | log1("entering listen mode: %s", |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1074 | new_mode != LISTEN_NONE |
| 1075 | ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw") |
| 1076 | : "none" |
| 1077 | ); |
| 1078 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1079 | client_data.listen_mode = new_mode; |
| 1080 | if (client_data.sockfd >= 0) { |
| 1081 | close(client_data.sockfd); |
| 1082 | client_data.sockfd = -1; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1083 | } |
| 1084 | if (new_mode == LISTEN_KERNEL) |
Denys Vlasenko | 84b89b4 | 2022-08-02 18:04:51 +0200 | [diff] [blame] | 1085 | client_data.sockfd = d6_listen_socket(CLIENT_PORT6, client_data.interface); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1086 | else if (new_mode != LISTEN_NONE) |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1087 | client_data.sockfd = d6_raw_socket(client_data.ifindex); |
| 1088 | /* else LISTEN_NONE: client_data.sockfd stays closed */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1091 | static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) |
| 1092 | { |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1093 | change_listen_mode(LISTEN_NONE); |
| 1094 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1095 | /* send release packet */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1096 | if (client_data.state == BOUND |
| 1097 | || client_data.state == RENEWING |
| 1098 | || client_data.state == REBINDING |
| 1099 | || client_data.state == RENEW_REQUESTED |
Denys Vlasenko | 44399e0 | 2016-07-03 20:26:44 +0200 | [diff] [blame] | 1100 | ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1101 | bb_simple_info_msg("unicasting a release"); |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1102 | client_data.xid = random_xid(); //TODO: can omit? |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1103 | send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1104 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1105 | bb_simple_info_msg("entering released state"); |
Peter Korsgaard | b6355e2 | 2016-08-26 18:46:34 +0200 | [diff] [blame] | 1106 | /* |
| 1107 | * We can be here on: SIGUSR2, |
| 1108 | * or on exit (SIGTERM) and -R "release on quit" is specified. |
| 1109 | * Users requested to be notified in all cases, even if not in one |
| 1110 | * of the states above. |
| 1111 | */ |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1112 | d6_run_script_no_option("deconfig"); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1113 | client_data.state = RELEASED; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1116 | #if BB_MMU |
| 1117 | static void client_background(void) |
| 1118 | { |
| 1119 | bb_daemonize(0); |
| 1120 | logmode &= ~LOGMODE_STDIO; |
| 1121 | /* rewrite pidfile, as our pid is different now */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1122 | write_pidfile(client_data.pidfile); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1123 | } |
| 1124 | #endif |
| 1125 | |
| 1126 | //usage:#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 1127 | //usage:# define IF_UDHCP_VERBOSE(...) __VA_ARGS__ |
| 1128 | //usage:#else |
| 1129 | //usage:# define IF_UDHCP_VERBOSE(...) |
| 1130 | //usage:#endif |
| 1131 | //usage:#define udhcpc6_trivial_usage |
Denys Vlasenko | 609df6f | 2021-06-16 14:24:27 +0200 | [diff] [blame] | 1132 | //usage: "[-fbq"IF_UDHCP_VERBOSE("v")"R] [-t N] [-T SEC] [-A SEC|-n] [-i IFACE] [-s PROG]\n" |
| 1133 | //usage: " [-p PIDFILE]"IF_FEATURE_UDHCP_PORT(" [-P PORT]")" [-ldo] [-r IPv6] [-x OPT:VAL]... [-O OPT]..." |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1134 | //usage:#define udhcpc6_full_usage "\n" |
Andre Kalb | 5f2e00f | 2020-05-05 18:24:25 +0200 | [diff] [blame] | 1135 | //usage: "\n -i IFACE Interface to use (default "CONFIG_UDHCPC_DEFAULT_INTERFACE")" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1136 | //usage: "\n -p FILE Create pidfile" |
Denys Vlasenko | 242d056 | 2022-12-15 11:51:16 +0100 | [diff] [blame] | 1137 | //usage: "\n -s PROG Run PROG at DHCP events (default "CONFIG_UDHCPC6_DEFAULT_SCRIPT")" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1138 | //usage: "\n -B Request broadcast replies" |
| 1139 | //usage: "\n -t N Send up to N discover packets" |
Denys Vlasenko | 609df6f | 2021-06-16 14:24:27 +0200 | [diff] [blame] | 1140 | //usage: "\n -T SEC Pause between packets (default 3)" |
| 1141 | //usage: "\n -A SEC Wait if lease is not obtained (default 20)" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1142 | //usage: USE_FOR_MMU( |
| 1143 | //usage: "\n -b Background if lease is not obtained" |
| 1144 | //usage: ) |
| 1145 | //usage: "\n -n Exit if lease is not obtained" |
| 1146 | //usage: "\n -q Exit after obtaining lease" |
| 1147 | //usage: "\n -R Release IP on exit" |
Denys Vlasenko | 609df6f | 2021-06-16 14:24:27 +0200 | [diff] [blame] | 1148 | //usage: "\n -f Run in foreground" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1149 | //usage: "\n -S Log to syslog too" |
| 1150 | //usage: IF_FEATURE_UDHCP_PORT( |
Denys Vlasenko | 8cc3a74 | 2020-12-18 22:51:46 +0100 | [diff] [blame] | 1151 | //usage: "\n -P PORT Use PORT (default 546)" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1152 | //usage: ) |
| 1153 | ////usage: IF_FEATURE_UDHCPC_ARPING( |
| 1154 | ////usage: "\n -a Use arping to validate offered address" |
| 1155 | ////usage: ) |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1156 | //usage: "\n -l Send 'information request' instead of 'solicit'" |
| 1157 | //usage: "\n (used for servers which do not assign IPv6 addresses)" |
Denys Vlasenko | 609df6f | 2021-06-16 14:24:27 +0200 | [diff] [blame] | 1158 | //usage: "\n -r IPv6 Request this address ('no' to not request any IP)" |
| 1159 | //usage: "\n -d Request prefix" |
| 1160 | //usage: "\n -o Don't request any options (unless -O is given)" |
| 1161 | //usage: "\n -O OPT Request option OPT from server (cumulative)" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1162 | //usage: "\n -x OPT:VAL Include option OPT in sent packets (cumulative)" |
| 1163 | //usage: "\n Examples of string, numeric, and hex byte opts:" |
| 1164 | //usage: "\n -x hostname:bbox - option 12" |
| 1165 | //usage: "\n -x lease:3600 - option 51 (lease time)" |
| 1166 | //usage: "\n -x 0x3d:0100BEEFC0FFEE - option 61 (client id)" |
Denys Vlasenko | 266f6f1 | 2018-04-13 13:18:34 +0200 | [diff] [blame] | 1167 | //usage: "\n -x 14:'\"dumpfile\"' - option 14 (shell-quoted)" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1168 | //usage: IF_UDHCP_VERBOSE( |
| 1169 | //usage: "\n -v Verbose" |
| 1170 | //usage: ) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1171 | //usage: "\nSignals:" |
| 1172 | //usage: "\n USR1 Renew lease" |
| 1173 | //usage: "\n USR2 Release lease" |
| 1174 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1175 | int udhcpc6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 1176 | int udhcpc6_main(int argc UNUSED_PARAM, char **argv) |
| 1177 | { |
| 1178 | const char *str_r; |
| 1179 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 1180 | uint8_t *clientid_mac_ptr; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1181 | llist_t *list_O = NULL; |
| 1182 | llist_t *list_x = NULL; |
| 1183 | int tryagain_timeout = 20; |
| 1184 | int discover_timeout = 3; |
| 1185 | int discover_retries = 3; |
| 1186 | struct in6_addr srv6_buf; |
| 1187 | struct in6_addr ipv6_buf; |
| 1188 | struct in6_addr *requested_ipv6; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1189 | int packet_num; |
| 1190 | int timeout; /* must be signed */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1191 | int lease_remaining; /* must be signed */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1192 | unsigned opt; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1193 | int retval; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1194 | |
Denys Vlasenko | df70a43 | 2016-04-21 18:54:36 +0200 | [diff] [blame] | 1195 | setup_common_bufsiz(); |
Denys Vlasenko | f6dd9e0 | 2018-01-19 18:44:19 +0100 | [diff] [blame] | 1196 | /* We want random_xid to be random */ |
| 1197 | srand(monotonic_us()); |
Denys Vlasenko | df70a43 | 2016-04-21 18:54:36 +0200 | [diff] [blame] | 1198 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1199 | /* Default options */ |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 1200 | IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;) |
| 1201 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;) |
Andre Kalb | 5f2e00f | 2020-05-05 18:24:25 +0200 | [diff] [blame] | 1202 | client_data.interface = CONFIG_UDHCPC_DEFAULT_INTERFACE; |
Denys Vlasenko | 242d056 | 2022-12-15 11:51:16 +0100 | [diff] [blame] | 1203 | client_data.script = CONFIG_UDHCPC6_DEFAULT_SCRIPT; |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1204 | client_data.sockfd = -1; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1205 | |
Denys Vlasenko | 65c34c5 | 2019-05-31 23:39:22 +0200 | [diff] [blame] | 1206 | /* Make sure fd 0,1,2 are open */ |
| 1207 | /* Set up the signal pipe on fds 3,4 - must be before openlog() */ |
| 1208 | udhcp_sp_setup(); |
| 1209 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1210 | /* Parse command line */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1211 | opt = getopt32long(argv, "^" |
| 1212 | /* O,x: list; -T,-t,-A take numeric param */ |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1213 | "i:np:qRr:s:T:+t:+SA:+O:*ox:*fld" |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1214 | USE_FOR_MMU("b") |
| 1215 | ///IF_FEATURE_UDHCPC_ARPING("a") |
| 1216 | IF_FEATURE_UDHCP_PORT("P:") |
| 1217 | "v" |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1218 | "\0" IF_UDHCP_VERBOSE("vv") /* -v is a counter */ |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 1219 | , udhcpc6_longopts |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1220 | , &client_data.interface, &client_data.pidfile, &str_r /* i,p */ |
| 1221 | , &client_data.script /* s */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1222 | , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ |
| 1223 | , &list_O |
| 1224 | , &list_x |
| 1225 | IF_FEATURE_UDHCP_PORT(, &str_P) |
| 1226 | IF_UDHCP_VERBOSE(, &dhcp_verbose) |
Denys Vlasenko | 7e21f04 | 2011-11-08 11:39:41 +0100 | [diff] [blame] | 1227 | ); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1228 | requested_ipv6 = NULL; |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1229 | option_mask32 |= OPT_r; |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1230 | if (opt & OPT_l) { |
| 1231 | /* for -l, do not require IPv6 assignment from server */ |
| 1232 | option_mask32 &= ~OPT_r; |
| 1233 | } else if (opt & OPT_r) { |
| 1234 | /* explicit "-r ARG" given */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1235 | if (strcmp(str_r, "no") == 0) { |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1236 | option_mask32 &= ~OPT_r; |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1237 | } else { |
| 1238 | if (inet_pton(AF_INET6, str_r, &ipv6_buf) <= 0) |
| 1239 | bb_error_msg_and_die("bad IPv6 address '%s'", str_r); |
| 1240 | requested_ipv6 = &ipv6_buf; |
| 1241 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1242 | } |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1243 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1244 | #if ENABLE_FEATURE_UDHCP_PORT |
| 1245 | if (opt & OPT_P) { |
Denys Vlasenko | 04ac6e0 | 2013-01-28 15:25:35 +0100 | [diff] [blame] | 1246 | CLIENT_PORT6 = xatou16(str_P); |
| 1247 | SERVER_PORT6 = CLIENT_PORT6 + 1; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1248 | } |
| 1249 | #endif |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1250 | while (list_O) { |
| 1251 | char *optstr = llist_pop(&list_O); |
| 1252 | unsigned n = bb_strtou(optstr, NULL, 0); |
| 1253 | if (errno || n > 254) { |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 1254 | n = udhcp_option_idx(optstr, d6_option_strings); |
| 1255 | n = d6_optflags[n].code; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1256 | } |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1257 | client_data.opt_mask[n >> 3] |= 1 << (n & 7); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1258 | } |
Denys Vlasenko | 293c945 | 2012-07-27 13:25:07 +0200 | [diff] [blame] | 1259 | if (!(opt & OPT_o)) { |
Denys Vlasenko | 293c945 | 2012-07-27 13:25:07 +0200 | [diff] [blame] | 1260 | unsigned i, n; |
Denys Vlasenko | ba4fbca | 2017-06-28 19:18:17 +0200 | [diff] [blame] | 1261 | for (i = 0; (n = d6_optflags[i].code) != 0; i++) { |
| 1262 | if (d6_optflags[i].flags & OPTION_REQ) { |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1263 | client_data.opt_mask[n >> 3] |= 1 << (n & 7); |
Denys Vlasenko | 293c945 | 2012-07-27 13:25:07 +0200 | [diff] [blame] | 1264 | } |
| 1265 | } |
Denys Vlasenko | 293c945 | 2012-07-27 13:25:07 +0200 | [diff] [blame] | 1266 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1267 | while (list_x) { |
Denys Vlasenko | 266f6f1 | 2018-04-13 13:18:34 +0200 | [diff] [blame] | 1268 | char *optstr = xstrdup(llist_pop(&list_x)); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1269 | udhcp_str2optset(optstr, &client_data.options, |
Denys Vlasenko | 6027597 | 2018-05-14 11:06:35 +0200 | [diff] [blame] | 1270 | d6_optflags, d6_option_strings, |
| 1271 | /*dhcpv6:*/ 1 |
| 1272 | ); |
Denys Vlasenko | 266f6f1 | 2018-04-13 13:18:34 +0200 | [diff] [blame] | 1273 | free(optstr); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 1276 | clientid_mac_ptr = NULL; |
Denys Vlasenko | e67b80f | 2021-12-12 17:13:54 +0100 | [diff] [blame] | 1277 | if (!udhcp_find_option(client_data.options, D6_OPT_CLIENTID, /*dhcpv6:*/ 1)) { |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 1278 | /* not set, set the default client ID */ |
Denys Vlasenko | 698cdef | 2021-06-02 15:07:46 +0200 | [diff] [blame] | 1279 | clientid_mac_ptr = udhcp_insert_new_option( |
| 1280 | &client_data.options, D6_OPT_CLIENTID, |
Denys Vlasenko | 949e962 | 2021-06-02 15:51:50 +0200 | [diff] [blame] | 1281 | 2+2 + 6, /*dhcp6:*/ 1); |
| 1282 | clientid_mac_ptr += 2+2; /* skip option code, len */ |
| 1283 | clientid_mac_ptr[1] = 3; /* DUID-LL */ |
| 1284 | clientid_mac_ptr[3] = 1; /* type: ethernet */ |
| 1285 | clientid_mac_ptr += 2+2; /* skip DUID-LL, ethernet */ |
| 1286 | } |
| 1287 | |
| 1288 | if (d6_read_interface(client_data.interface, |
| 1289 | &client_data.ifindex, |
| 1290 | &client6_data.ll_ip6, |
| 1291 | client_data.client_mac) |
| 1292 | ) { |
| 1293 | return 1; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | #if !BB_MMU |
| 1297 | /* on NOMMU reexec (i.e., background) early */ |
| 1298 | if (!(opt & OPT_f)) { |
| 1299 | bb_daemonize_or_rexec(0 /* flags */, argv); |
| 1300 | logmode = LOGMODE_NONE; |
| 1301 | } |
| 1302 | #endif |
| 1303 | if (opt & OPT_S) { |
| 1304 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
| 1305 | logmode |= LOGMODE_SYSLOG; |
| 1306 | } |
| 1307 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1308 | /* Create pidfile */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1309 | write_pidfile(client_data.pidfile); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1310 | /* Goes to stdout (unless NOMMU) and possibly syslog */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1311 | bb_simple_info_msg("started, v"BB_VER); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1312 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1313 | client_data.state = INIT_SELECTING; |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1314 | d6_run_script_no_option("deconfig"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1315 | packet_num = 0; |
| 1316 | timeout = 0; |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1317 | lease_remaining = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1318 | |
| 1319 | /* Main event loop. select() waits on signal pipe and possibly |
| 1320 | * on sockfd. |
| 1321 | * "continue" statements in code below jump to the top of the loop. |
| 1322 | */ |
| 1323 | for (;;) { |
Denys Vlasenko | 52a515d | 2017-02-16 23:25:44 +0100 | [diff] [blame] | 1324 | struct pollfd pfds[2]; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1325 | struct d6_packet packet; |
| 1326 | uint8_t *packet_end; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1327 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1328 | //bb_error_msg("sockfd:%d, listen_mode:%d", client_data.sockfd, client_data.listen_mode); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1329 | |
| 1330 | /* Was opening raw or udp socket here |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1331 | * if (client_data.listen_mode != LISTEN_NONE && client_data.sockfd < 0), |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1332 | * but on fast network renew responses return faster |
| 1333 | * than we open sockets. Thus this code is moved |
| 1334 | * to change_listen_mode(). Thus we open listen socket |
| 1335 | * BEFORE we send renew request (see "case BOUND:"). */ |
| 1336 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1337 | udhcp_sp_fd_set(pfds, client_data.sockfd); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1338 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1339 | retval = 0; |
| 1340 | /* If we already timed out, fall through with retval = 0, else... */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1341 | if (timeout > 0) { |
| 1342 | unsigned diff; |
| 1343 | |
| 1344 | if (timeout > INT_MAX/1000) |
| 1345 | timeout = INT_MAX/1000; |
| 1346 | log1("waiting %u seconds", timeout); |
| 1347 | diff = (unsigned)monotonic_sec(); |
| 1348 | retval = poll(pfds, 2, timeout * 1000); |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1349 | diff = (unsigned)monotonic_sec() - diff; |
| 1350 | lease_remaining -= diff; |
| 1351 | if (lease_remaining < 0) |
| 1352 | lease_remaining = 0; |
| 1353 | timeout -= diff; |
| 1354 | if (timeout < 0) |
| 1355 | timeout = 0; |
| 1356 | |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1357 | if (retval < 0) { |
| 1358 | /* EINTR? A signal was caught, don't panic */ |
| 1359 | if (errno == EINTR) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1360 | continue; |
| 1361 | } |
| 1362 | /* Else: an error occured, panic! */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1363 | bb_simple_perror_msg_and_die("poll"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* If timeout dropped to zero, time to become active: |
| 1368 | * resend discover/renew/whatever |
| 1369 | */ |
| 1370 | if (retval == 0) { |
| 1371 | /* When running on a bridge, the ifindex may have changed |
| 1372 | * (e.g. if member interfaces were added/removed |
| 1373 | * or if the status of the bridge changed). |
| 1374 | * Refresh ifindex and client_mac: |
| 1375 | */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1376 | if (d6_read_interface(client_data.interface, |
| 1377 | &client_data.ifindex, |
Denys Vlasenko | e09f5e3 | 2017-03-27 22:10:15 +0200 | [diff] [blame] | 1378 | &client6_data.ll_ip6, |
Denys Vlasenko | 949e962 | 2021-06-02 15:51:50 +0200 | [diff] [blame] | 1379 | client_data.client_mac) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1380 | ) { |
| 1381 | goto ret0; /* iface is gone? */ |
| 1382 | } |
Denys Vlasenko | e09f5e3 | 2017-03-27 22:10:15 +0200 | [diff] [blame] | 1383 | |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 1384 | if (clientid_mac_ptr) |
Denys Vlasenko | 949e962 | 2021-06-02 15:51:50 +0200 | [diff] [blame] | 1385 | memcpy(clientid_mac_ptr, client_data.client_mac, 6); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1386 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1387 | switch (client_data.state) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1388 | case INIT_SELECTING: |
Felix Fietkau | 1c7a58d | 2012-09-27 16:22:24 +0200 | [diff] [blame] | 1389 | if (!discover_retries || packet_num < discover_retries) { |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1390 | if (packet_num == 0) { |
| 1391 | change_listen_mode(LISTEN_RAW); |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1392 | client_data.xid = random_xid(); |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1393 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1394 | /* multicast */ |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1395 | if (opt & OPT_l) |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1396 | send_d6_info_request(); |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1397 | else |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1398 | send_d6_discover(requested_ipv6); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1399 | timeout = discover_timeout; |
| 1400 | packet_num++; |
| 1401 | continue; |
| 1402 | } |
| 1403 | leasefail: |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1404 | change_listen_mode(LISTEN_NONE); |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1405 | d6_run_script_no_option("leasefail"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1406 | #if BB_MMU /* -b is not supported on NOMMU */ |
| 1407 | if (opt & OPT_b) { /* background if no lease */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1408 | bb_simple_info_msg("no lease, forking to background"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1409 | client_background(); |
| 1410 | /* do not background again! */ |
Andrey Mazo | 87e2162 | 2019-02-22 16:46:19 -0500 | [diff] [blame] | 1411 | opt = ((opt & ~(OPT_b|OPT_n)) | OPT_f); |
| 1412 | /* ^^^ also disables -n (-b takes priority over -n): |
| 1413 | * ifup's default udhcpc options are -R -n, |
| 1414 | * and users want to be able to add -b |
| 1415 | * (in a config file) to make it background |
| 1416 | * _and not exit_. |
| 1417 | */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1418 | } else |
| 1419 | #endif |
| 1420 | if (opt & OPT_n) { /* abort if no lease */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1421 | bb_simple_info_msg("no lease, failing"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1422 | retval = 1; |
| 1423 | goto ret; |
| 1424 | } |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1425 | /* Wait before trying again */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1426 | timeout = tryagain_timeout; |
| 1427 | packet_num = 0; |
| 1428 | continue; |
| 1429 | case REQUESTING: |
Felix Fietkau | 1c7a58d | 2012-09-27 16:22:24 +0200 | [diff] [blame] | 1430 | if (!discover_retries || packet_num < discover_retries) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1431 | /* send multicast select packet */ |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1432 | send_d6_select(); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1433 | timeout = discover_timeout; |
| 1434 | packet_num++; |
| 1435 | continue; |
| 1436 | } |
| 1437 | /* Timed out, go back to init state. |
| 1438 | * "discover...select...discover..." loops |
| 1439 | * were seen in the wild. Treat them similarly |
| 1440 | * to "no response to discover" case */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1441 | client_data.state = INIT_SELECTING; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1442 | goto leasefail; |
| 1443 | case BOUND: |
| 1444 | /* 1/2 lease passed, enter renewing state */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1445 | client_data.state = RENEWING; |
| 1446 | client_data.first_secs = 0; /* make secs field count from 0 */ |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1447 | got_SIGUSR1: |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1448 | log1s("entering renew state"); |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1449 | change_listen_mode(LISTEN_KERNEL); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1450 | /* fall right through */ |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1451 | case RENEW_REQUESTED: /* in manual (SIGUSR1) renew */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1452 | case RENEWING: |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1453 | if (packet_num == 0) { |
| 1454 | /* Send an unicast renew request */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1455 | /* Sometimes observed to fail (EADDRNOTAVAIL) to bind |
| 1456 | * a new UDP socket for sending inside send_renew. |
| 1457 | * I hazard to guess existing listening socket |
| 1458 | * is somehow conflicting with it, but why is it |
| 1459 | * not deterministic then?! Strange. |
| 1460 | * Anyway, it does recover by eventually failing through |
| 1461 | * into INIT_SELECTING state. |
| 1462 | */ |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1463 | if (opt & OPT_l) |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1464 | send_d6_info_request(); |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1465 | else |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1466 | send_d6_renew(&srv6_buf, requested_ipv6); |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1467 | timeout = discover_timeout; |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1468 | packet_num++; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1469 | continue; |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1470 | } /* else: we had sent one packet, but got no reply */ |
| 1471 | log1s("no response to renew"); |
| 1472 | if (lease_remaining > 30) { |
| 1473 | /* Some lease time remains, try to renew later */ |
| 1474 | change_listen_mode(LISTEN_NONE); |
| 1475 | goto BOUND_for_half_lease; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1476 | } |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1477 | /* Enter rebinding state */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1478 | client_data.state = REBINDING; |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1479 | log1s("entering rebinding state"); |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1480 | /* Switch to bcast receive */ |
| 1481 | change_listen_mode(LISTEN_RAW); |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1482 | packet_num = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1483 | /* fall right through */ |
| 1484 | case REBINDING: |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1485 | /* Lease is *really* about to run out, |
| 1486 | * try to find DHCP server using broadcast */ |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1487 | if (lease_remaining > 0 && packet_num < 3) { |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1488 | if (opt & OPT_l) |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1489 | send_d6_info_request(); |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1490 | else /* send a broadcast renew request */ |
Denys Vlasenko | 75aaa8b | 2022-08-02 15:33:51 +0200 | [diff] [blame] | 1491 | //TODO: send_d6_renew uses D6_MSG_RENEW message, should we use D6_MSG_REBIND here instead? |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1492 | send_d6_renew(/*server_ipv6:*/ NULL, requested_ipv6); |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1493 | timeout = discover_timeout; |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1494 | packet_num++; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1495 | continue; |
| 1496 | } |
| 1497 | /* Timed out, enter init state */ |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1498 | change_listen_mode(LISTEN_NONE); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1499 | bb_simple_info_msg("lease lost, entering init state"); |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1500 | d6_run_script_no_option("deconfig"); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1501 | client_data.state = INIT_SELECTING; |
| 1502 | client_data.first_secs = 0; /* make secs field count from 0 */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1503 | timeout = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1504 | packet_num = 0; |
| 1505 | continue; |
| 1506 | /* case RELEASED: */ |
| 1507 | } |
Denys Vlasenko | 265fcdd | 2021-06-02 13:50:26 +0200 | [diff] [blame] | 1508 | /* RELEASED state (when we got SIGUSR2) ends up here. |
| 1509 | * (wait for SIGUSR1 to re-init, or for TERM, etc) |
| 1510 | */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1511 | timeout = INT_MAX; |
| 1512 | continue; /* back to main loop */ |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 1513 | } /* if poll timed out */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1514 | |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 1515 | /* poll() didn't timeout, something happened */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1516 | |
| 1517 | /* Is it a signal? */ |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 1518 | switch (udhcp_sp_read()) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1519 | case SIGUSR1: |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1520 | if (client_data.state <= REQUESTING) |
| 1521 | /* Initial negotiations in progress, do not disturb */ |
| 1522 | break; |
Denys Vlasenko | ecaf8e8 | 2021-06-03 16:22:35 +0200 | [diff] [blame] | 1523 | if (client_data.state == REBINDING) |
| 1524 | /* Do not go back from rebind to renew state */ |
| 1525 | break; |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1526 | |
| 1527 | if (lease_remaining > 30) /* if renew fails, do not go back to BOUND */ |
| 1528 | lease_remaining = 30; |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1529 | client_data.first_secs = 0; /* make secs field count from 0 */ |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1530 | packet_num = 0; |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1531 | |
| 1532 | switch (client_data.state) { |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1533 | case BOUND: |
| 1534 | case RENEWING: |
Denys Vlasenko | ecaf8e8 | 2021-06-03 16:22:35 +0200 | [diff] [blame] | 1535 | /* Try to renew/rebind */ |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1536 | change_listen_mode(LISTEN_KERNEL); |
| 1537 | client_data.state = RENEW_REQUESTED; |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1538 | goto got_SIGUSR1; |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1539 | |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1540 | case RENEW_REQUESTED: |
Denys Vlasenko | ecaf8e8 | 2021-06-03 16:22:35 +0200 | [diff] [blame] | 1541 | /* Two SIGUSR1 received, start things over */ |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1542 | change_listen_mode(LISTEN_NONE); |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1543 | d6_run_script_no_option("deconfig"); |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1544 | |
| 1545 | default: |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1546 | /* case RELEASED: */ |
Denys Vlasenko | ecaf8e8 | 2021-06-03 16:22:35 +0200 | [diff] [blame] | 1547 | /* Wake from SIGUSR2-induced deconfigured state */ |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1548 | change_listen_mode(LISTEN_NONE); |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1549 | } |
Denys Vlasenko | e6007c4 | 2021-05-01 13:48:50 +0200 | [diff] [blame] | 1550 | client_data.state = INIT_SELECTING; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1551 | /* Kill any timeouts, user wants this to hurry along */ |
| 1552 | timeout = 0; |
| 1553 | continue; |
| 1554 | case SIGUSR2: |
| 1555 | perform_d6_release(&srv6_buf, requested_ipv6); |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1556 | /* ^^^ switches to LISTEN_NONE */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1557 | timeout = INT_MAX; |
| 1558 | continue; |
| 1559 | case SIGTERM: |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1560 | bb_info_msg("received %s", "SIGTERM"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1561 | goto ret0; |
| 1562 | } |
| 1563 | |
| 1564 | /* Is it a packet? */ |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 1565 | if (!pfds[1].revents) |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1566 | continue; /* no */ |
| 1567 | |
| 1568 | { |
| 1569 | int len; |
| 1570 | |
| 1571 | /* A packet is ready, read it */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1572 | if (client_data.listen_mode == LISTEN_KERNEL) |
| 1573 | len = d6_recv_kernel_packet(&srv6_buf, &packet, client_data.sockfd); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1574 | else |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1575 | len = d6_recv_raw_packet(&srv6_buf, &packet, client_data.sockfd); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1576 | if (len == -1) { |
| 1577 | /* Error is severe, reopen socket */ |
Denys Vlasenko | 6f97b30 | 2017-09-29 18:17:25 +0200 | [diff] [blame] | 1578 | bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1579 | sleep(discover_timeout); /* 3 seconds by default */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1580 | change_listen_mode(client_data.listen_mode); /* just close and reopen */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1581 | } |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1582 | if (len < 0) |
| 1583 | continue; |
| 1584 | packet_end = (uint8_t*)&packet + len; |
| 1585 | } |
| 1586 | |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1587 | if ((packet.d6_xid32 & htonl(0x00ffffff)) != client_data.xid) { |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1588 | log1("xid %x (our is %x)%s", |
Denys Vlasenko | 827b690 | 2021-06-15 01:02:01 +0200 | [diff] [blame] | 1589 | (unsigned)(packet.d6_xid32 & htonl(0x00ffffff)), (unsigned)client_data.xid, |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1590 | ", ignoring packet" |
| 1591 | ); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1592 | continue; |
| 1593 | } |
| 1594 | |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1595 | switch (client_data.state) { |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1596 | case INIT_SELECTING: |
| 1597 | if (packet.d6_msg_type == D6_MSG_ADVERTISE) |
| 1598 | goto type_is_ok; |
| 1599 | /* DHCPv6 has "Rapid Commit", when instead of Advertise, |
| 1600 | * server sends Reply right away. |
| 1601 | * Fall through to check for this case. |
| 1602 | */ |
| 1603 | case REQUESTING: |
| 1604 | case RENEWING: |
| 1605 | case RENEW_REQUESTED: |
| 1606 | case REBINDING: |
| 1607 | if (packet.d6_msg_type == D6_MSG_REPLY) { |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1608 | unsigned start; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1609 | uint32_t lease_seconds; |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1610 | struct d6_option *option; |
Denys Vlasenko | 7c44b60 | 2018-01-17 13:55:51 +0100 | [diff] [blame] | 1611 | unsigned address_timeout; |
| 1612 | unsigned prefix_timeout; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1613 | type_is_ok: |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1614 | change_listen_mode(LISTEN_NONE); |
| 1615 | |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1616 | address_timeout = 0; |
| 1617 | prefix_timeout = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1618 | option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE); |
Denys Vlasenko | 64d58aa | 2017-03-27 22:22:09 +0200 | [diff] [blame] | 1619 | if (option && (option->data[0] | option->data[1]) != 0) { |
Denys Vlasenko | e977853 | 2022-12-14 00:36:59 +0100 | [diff] [blame] | 1620 | ///FIXME: |
| 1621 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1622 | // | OPTION_STATUS_CODE | option-len | |
| 1623 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1624 | // | status-code | | |
| 1625 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 1626 | // . status-message . |
| 1627 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1628 | // so why do we think it's NAK if data[0] is zero but data[1] is not? That's wrong... |
| 1629 | // we should also check that option->len is ok (i.e. not 0), right? |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1630 | /* return to init state */ |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1631 | bb_info_msg("received DHCP NAK (%u)", option->data[4]); |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1632 | d6_run_script(packet.d6_options, |
| 1633 | packet_end, "nak"); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1634 | if (client_data.state != REQUESTING) |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1635 | d6_run_script_no_option("deconfig"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1636 | sleep(3); /* avoid excessive network traffic */ |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1637 | client_data.state = INIT_SELECTING; |
| 1638 | client_data.first_secs = 0; /* make secs field count from 0 */ |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1639 | requested_ipv6 = NULL; |
| 1640 | timeout = 0; |
| 1641 | packet_num = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1642 | continue; |
| 1643 | } |
| 1644 | option = d6_copy_option(packet.d6_options, packet_end, D6_OPT_SERVERID); |
| 1645 | if (!option) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1646 | bb_simple_info_msg("no server ID, ignoring packet"); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1647 | continue; |
| 1648 | /* still selecting - this server looks bad */ |
| 1649 | } |
| 1650 | //Note: we do not bother comparing server IDs in Advertise and Reply msgs. |
| 1651 | //server_id variable is used solely for creation of proper server_id option |
| 1652 | //in outgoing packets. (why DHCPv6 even introduced it is a mystery). |
| 1653 | free(client6_data.server_id); |
| 1654 | client6_data.server_id = option; |
| 1655 | if (packet.d6_msg_type == D6_MSG_ADVERTISE) { |
| 1656 | /* enter requesting state */ |
Denys Vlasenko | 0ae5345 | 2021-06-02 16:49:20 +0200 | [diff] [blame] | 1657 | change_listen_mode(LISTEN_RAW); |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1658 | client_data.state = REQUESTING; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1659 | timeout = 0; |
| 1660 | packet_num = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1661 | continue; |
| 1662 | } |
| 1663 | /* It's a D6_MSG_REPLY */ |
| 1664 | /* |
| 1665 | * RFC 3315 18.1.8. Receipt of Reply Messages |
| 1666 | * |
| 1667 | * Upon the receipt of a valid Reply message in response to a Solicit |
| 1668 | * (with a Rapid Commit option), Request, Confirm, Renew, Rebind or |
| 1669 | * Information-request message, the client extracts the configuration |
| 1670 | * information contained in the Reply. The client MAY choose to report |
| 1671 | * any status code or message from the status code option in the Reply |
| 1672 | * message. |
| 1673 | * |
| 1674 | * The client SHOULD perform duplicate address detection [17] on each of |
| 1675 | * the addresses in any IAs it receives in the Reply message before |
| 1676 | * using that address for traffic. If any of the addresses are found to |
| 1677 | * be in use on the link, the client sends a Decline message to the |
| 1678 | * server as described in section 18.1.7. |
| 1679 | * |
| 1680 | * If the Reply was received in response to a Solicit (with a Rapid |
| 1681 | * Commit option), Request, Renew or Rebind message, the client updates |
| 1682 | * the information it has recorded about IAs from the IA options |
| 1683 | * contained in the Reply message: |
| 1684 | * |
| 1685 | * - Record T1 and T2 times. |
| 1686 | * |
| 1687 | * - Add any new addresses in the IA option to the IA as recorded by |
| 1688 | * the client. |
| 1689 | * |
| 1690 | * - Update lifetimes for any addresses in the IA option that the |
| 1691 | * client already has recorded in the IA. |
| 1692 | * |
| 1693 | * - Discard any addresses from the IA, as recorded by the client, that |
| 1694 | * have a valid lifetime of 0 in the IA Address option. |
| 1695 | * |
| 1696 | * - Leave unchanged any information about addresses the client has |
| 1697 | * recorded in the IA but that were not included in the IA from the |
| 1698 | * server. |
| 1699 | * |
| 1700 | * Management of the specific configuration information is detailed in |
| 1701 | * the definition of each option in section 22. |
| 1702 | * |
| 1703 | * If the client receives a Reply message with a Status Code containing |
| 1704 | * UnspecFail, the server is indicating that it was unable to process |
| 1705 | * the message due to an unspecified failure condition. If the client |
| 1706 | * retransmits the original message to the same server to retry the |
| 1707 | * desired operation, the client MUST limit the rate at which it |
| 1708 | * retransmits the message and limit the duration of the time during |
| 1709 | * which it retransmits the message. |
| 1710 | * |
| 1711 | * When the client receives a Reply message with a Status Code option |
| 1712 | * with the value UseMulticast, the client records the receipt of the |
| 1713 | * message and sends subsequent messages to the server through the |
| 1714 | * interface on which the message was received using multicast. The |
| 1715 | * client resends the original message using multicast. |
| 1716 | * |
| 1717 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1718 | * | OPTION_IA_NA | option-len | |
| 1719 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1720 | * | IAID (4 octets) | |
| 1721 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1722 | * | T1 | |
| 1723 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1724 | * | T2 | |
| 1725 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1726 | * | | |
| 1727 | * . IA_NA-options . |
| 1728 | * . . |
| 1729 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1730 | * |
| 1731 | * |
| 1732 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1733 | * | OPTION_IAADDR | option-len | |
| 1734 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1735 | * | | |
| 1736 | * | IPv6 address | |
| 1737 | * | | |
| 1738 | * | | |
| 1739 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1740 | * | preferred-lifetime | |
| 1741 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1742 | * | valid-lifetime | |
| 1743 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1744 | * . . |
| 1745 | * . IAaddr-options . |
| 1746 | * . . |
| 1747 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 1748 | */ |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1749 | if (option_mask32 & OPT_r) { |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1750 | struct d6_option *iaaddr; |
| 1751 | |
Denys Vlasenko | 1e8d79b | 2018-01-16 21:35:39 +0100 | [diff] [blame] | 1752 | free(client6_data.ia_na); |
| 1753 | client6_data.ia_na = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_NA); |
| 1754 | if (!client6_data.ia_na) { |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1755 | bb_info_msg("no %s option%s", "IA_NA", ", ignoring packet"); |
Denys Vlasenko | 1e8d79b | 2018-01-16 21:35:39 +0100 | [diff] [blame] | 1756 | continue; |
| 1757 | } |
| 1758 | if (client6_data.ia_na->len < (4 + 4 + 4) + (2 + 2 + 16 + 4 + 4)) { |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1759 | bb_info_msg("%s option is too short:%d bytes", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1760 | "IA_NA", client6_data.ia_na->len); |
Denys Vlasenko | 1e8d79b | 2018-01-16 21:35:39 +0100 | [diff] [blame] | 1761 | continue; |
| 1762 | } |
| 1763 | iaaddr = d6_find_option(client6_data.ia_na->data + 4 + 4 + 4, |
| 1764 | client6_data.ia_na->data + client6_data.ia_na->len, |
| 1765 | D6_OPT_IAADDR |
| 1766 | ); |
| 1767 | if (!iaaddr) { |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1768 | bb_info_msg("no %s option%s", "IAADDR", ", ignoring packet"); |
Denys Vlasenko | 1e8d79b | 2018-01-16 21:35:39 +0100 | [diff] [blame] | 1769 | continue; |
| 1770 | } |
| 1771 | if (iaaddr->len < (16 + 4 + 4)) { |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1772 | bb_info_msg("%s option is too short:%d bytes", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1773 | "IAADDR", iaaddr->len); |
Denys Vlasenko | 1e8d79b | 2018-01-16 21:35:39 +0100 | [diff] [blame] | 1774 | continue; |
| 1775 | } |
| 1776 | /* Note: the address is sufficiently aligned for cast: |
| 1777 | * we _copied_ IA-NA, and copy is always well-aligned. |
| 1778 | */ |
| 1779 | requested_ipv6 = (struct in6_addr*) iaaddr->data; |
| 1780 | move_from_unaligned32(lease_seconds, iaaddr->data + 16 + 4); |
| 1781 | lease_seconds = ntohl(lease_seconds); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1782 | /// TODO: check for 0 lease time? |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1783 | bb_info_msg("%s obtained, lease time %u", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1784 | "IPv6", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds); |
Denys Vlasenko | 7c44b60 | 2018-01-17 13:55:51 +0100 | [diff] [blame] | 1785 | address_timeout = lease_seconds; |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1786 | } |
| 1787 | if (option_mask32 & OPT_d) { |
| 1788 | struct d6_option *iaprefix; |
| 1789 | |
| 1790 | free(client6_data.ia_pd); |
| 1791 | client6_data.ia_pd = d6_copy_option(packet.d6_options, packet_end, D6_OPT_IA_PD); |
| 1792 | if (!client6_data.ia_pd) { |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1793 | bb_info_msg("no %s option%s", "IA_PD", ", ignoring packet"); |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1794 | continue; |
| 1795 | } |
| 1796 | if (client6_data.ia_pd->len < (4 + 4 + 4) + (2 + 2 + 4 + 4 + 1 + 16)) { |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1797 | bb_info_msg("%s option is too short:%d bytes", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1798 | "IA_PD", client6_data.ia_pd->len); |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1799 | continue; |
| 1800 | } |
| 1801 | iaprefix = d6_find_option(client6_data.ia_pd->data + 4 + 4 + 4, |
| 1802 | client6_data.ia_pd->data + client6_data.ia_pd->len, |
| 1803 | D6_OPT_IAPREFIX |
| 1804 | ); |
| 1805 | if (!iaprefix) { |
Denys Vlasenko | a4959ee | 2021-02-21 16:32:07 +0100 | [diff] [blame] | 1806 | bb_info_msg("no %s option%s", "IAPREFIX", ", ignoring packet"); |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1807 | continue; |
| 1808 | } |
| 1809 | if (iaprefix->len < (4 + 4 + 1 + 16)) { |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1810 | bb_info_msg("%s option is too short:%d bytes", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1811 | "IAPREFIX", iaprefix->len); |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1812 | continue; |
| 1813 | } |
| 1814 | move_from_unaligned32(lease_seconds, iaprefix->data + 4); |
| 1815 | lease_seconds = ntohl(lease_seconds); |
James Byrne | 253c4e7 | 2019-04-12 17:01:51 +0000 | [diff] [blame] | 1816 | bb_info_msg("%s obtained, lease time %u", |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1817 | "prefix", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds); |
Denys Vlasenko | 7c44b60 | 2018-01-17 13:55:51 +0100 | [diff] [blame] | 1818 | prefix_timeout = lease_seconds; |
Denys Vlasenko | ef5207f | 2018-01-16 21:39:14 +0100 | [diff] [blame] | 1819 | } |
Denys Vlasenko | 6e9e6d8 | 2018-01-16 21:52:23 +0100 | [diff] [blame] | 1820 | if (!address_timeout) |
| 1821 | address_timeout = prefix_timeout; |
| 1822 | if (!prefix_timeout) |
| 1823 | prefix_timeout = address_timeout; |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1824 | lease_remaining = (prefix_timeout < address_timeout ? prefix_timeout : address_timeout); |
| 1825 | if (lease_remaining < 0) /* signed overflow? */ |
| 1826 | lease_remaining = INT_MAX; |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1827 | if (opt & OPT_l) { |
| 1828 | /* TODO: request OPTION_INFORMATION_REFRESH_TIME (32) |
| 1829 | * and use its value instead of the default 1 day. |
| 1830 | */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1831 | lease_remaining = 24 * 60 * 60; |
Eivind Versvik | 22a6339 | 2019-08-24 17:23:48 +0200 | [diff] [blame] | 1832 | } |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1833 | /* paranoia: must not be too small */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1834 | if (lease_remaining < 30) |
| 1835 | lease_remaining = 30; |
| 1836 | |
Denys Vlasenko | 14f1320 | 2018-01-16 21:47:10 +0100 | [diff] [blame] | 1837 | /* enter bound state */ |
Denys Vlasenko | a1a77ad | 2021-05-01 11:54:08 +0200 | [diff] [blame] | 1838 | start = monotonic_sec(); |
David Decotigny | 8f48fc0 | 2018-05-24 08:30:15 -0700 | [diff] [blame] | 1839 | d6_run_script(packet.d6_options, packet_end, |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1840 | (client_data.state == REQUESTING ? "bound" : "renew")); |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1841 | lease_remaining -= (unsigned)monotonic_sec() - start; |
| 1842 | if (lease_remaining < 0) |
| 1843 | lease_remaining = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1844 | if (opt & OPT_q) { /* quit after lease */ |
| 1845 | goto ret0; |
| 1846 | } |
| 1847 | /* future renew failures should not exit (JM) */ |
| 1848 | opt &= ~OPT_n; |
| 1849 | #if BB_MMU /* NOMMU case backgrounded earlier */ |
| 1850 | if (!(opt & OPT_f)) { |
| 1851 | client_background(); |
| 1852 | /* do not background again! */ |
| 1853 | opt = ((opt & ~OPT_b) | OPT_f); |
| 1854 | } |
| 1855 | #endif |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1856 | |
Denys Vlasenko | 1c72537 | 2021-06-03 09:20:45 +0200 | [diff] [blame] | 1857 | BOUND_for_half_lease: |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1858 | timeout = (unsigned)lease_remaining / 2; |
| 1859 | client_data.state = BOUND; |
Denys Vlasenko | 687f41f | 2021-06-01 00:19:03 +0200 | [diff] [blame] | 1860 | packet_num = 0; |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1861 | continue; /* back to main loop */ |
| 1862 | } |
| 1863 | continue; |
| 1864 | /* case BOUND: - ignore all packets */ |
| 1865 | /* case RELEASED: - ignore all packets */ |
| 1866 | } |
| 1867 | /* back to main loop */ |
| 1868 | } /* for (;;) - main loop ends */ |
| 1869 | |
| 1870 | ret0: |
| 1871 | if (opt & OPT_R) /* release on quit */ |
| 1872 | perform_d6_release(&srv6_buf, requested_ipv6); |
| 1873 | retval = 0; |
| 1874 | ret: |
Denys Vlasenko | bcb1fc3 | 2019-05-26 15:01:13 +0200 | [diff] [blame] | 1875 | /*if (client_data.pidfile) - remove_pidfile has its own check */ |
| 1876 | remove_pidfile(client_data.pidfile); |
Denys Vlasenko | 9ba7504 | 2011-11-07 15:55:39 +0100 | [diff] [blame] | 1877 | return retval; |
Denys Vlasenko | abe248b | 2019-05-15 14:19:46 +0200 | [diff] [blame] | 1878 | } |