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