From: Denys Vlasenko Date: Wed, 28 Jun 2017 17:18:17 +0000 (+0200) Subject: udhcpc6: make -O OPT work X-Git-Tag: 1_27_0~5 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=ba4fbca8a81d765f81aefc74db7f73ec9ded3550;p=oweals%2Fbusybox.git udhcpc6: make -O OPT work Patch is based on work by tiggerswelt.net. Signed-off-by: Denys Vlasenko --- diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 420695a20..d3eea5def 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -14,6 +14,7 @@ const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; +#if ENABLE_UDHCPC || ENABLE_UDHCPD /* Supported options are easily added here. * See RFC2132 for more options. * OPTION_REQ: these options are requested by udhcpc (unless -o). @@ -136,6 +137,7 @@ const char dhcp_option_strings[] ALIGN1 = "msstaticroutes""\0"/* DHCP_MS_STATIC_ROUTES */ "wpad" "\0" /* DHCP_WPAD */ ; +#endif /* Lengths of the option types in binary form. * Used by: @@ -190,21 +192,26 @@ static void log_option(const char *pfx, const uint8_t *opt) # define log_option(pfx, opt) ((void)0) #endif -unsigned FAST_FUNC udhcp_option_idx(const char *name) +unsigned FAST_FUNC udhcp_option_idx(const char *name, const char *option_strings) { - int n = index_in_strings(dhcp_option_strings, name); + int n = index_in_strings(option_strings, name); if (n >= 0) return n; { - char buf[sizeof(dhcp_option_strings)]; - char *d = buf; - const char *s = dhcp_option_strings; - while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) { + char *buf, *d; + const char *s; + + s = option_strings; + while (*s) + s += strlen(s) + 1; + + d = buf = xzalloc(s - option_strings); + s = option_strings; + while (!(*s == '\0' && s[1] == '\0')) { *d++ = (*s == '\0' ? ' ' : *s); s++; } - *d = '\0'; bb_error_msg_and_die("unknown option '%s', known options: %s", name, buf); } } @@ -315,6 +322,7 @@ void FAST_FUNC udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addo optionptr[end + len] = DHCP_END; } +#if ENABLE_UDHCPC || ENABLE_UDHCPD /* Add an one to four byte option to a packet */ void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) { @@ -338,6 +346,7 @@ void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, bb_error_msg("can't add option 0x%02x", code); } +#endif /* Find option 'code' in opt_list */ struct option_set* FAST_FUNC udhcp_find_option(struct option_set *opt_list, uint8_t code) @@ -451,7 +460,7 @@ static NOINLINE void attach_option( free(allocated); } -int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg) +int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg, const struct dhcp_optflag *optflags, const char *option_strings) { struct option_set **opt_list = arg; char *opt, *val; @@ -478,7 +487,7 @@ int FAST_FUNC udhcp_str2optset(const char *const_str, void *arg) bin_optflag.code = optcode; optflag = &bin_optflag; } else { - optflag = &dhcp_optflags[udhcp_option_idx(opt)]; + optflag = &optflags[udhcp_option_idx(opt, option_strings)]; } retval = 0; diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index ee12cf91b..6907e7f60 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h @@ -93,8 +93,10 @@ enum { OPTION_BIN, OPTION_STATIC_ROUTES, OPTION_6RD, -#if ENABLE_FEATURE_UDHCP_RFC3397 +#if ENABLE_FEATURE_UDHCP_RFC3397 || ENABLE_FEATURE_UDHCPC6_RFC3646 || ENABLE_FEATURE_UDHCPC6_RFC4704 OPTION_DNS_STRING, /* RFC1035 compressed domain name list */ +#endif +#if ENABLE_FEATURE_UDHCP_RFC3397 OPTION_SIP_SERVERS, #endif @@ -189,17 +191,21 @@ struct option_set { struct option_set *next; }; +#if ENABLE_UDHCPC || ENABLE_UDHCPD extern const struct dhcp_optflag dhcp_optflags[]; extern const char dhcp_option_strings[] ALIGN1; +#endif extern const uint8_t dhcp_option_lengths[] ALIGN1; -unsigned FAST_FUNC udhcp_option_idx(const char *name); +unsigned FAST_FUNC udhcp_option_idx(const char *name, const char *option_strings); uint8_t *udhcp_get_option(struct dhcp_packet *packet, int code) FAST_FUNC; int udhcp_end_option(uint8_t *optionptr) FAST_FUNC; void udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) FAST_FUNC; +#if ENABLE_UDHCPC || ENABLE_UDHCPD void udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) FAST_FUNC; -#if ENABLE_FEATURE_UDHCP_RFC3397 +#endif +#if ENABLE_FEATURE_UDHCP_RFC3397 || ENABLE_FEATURE_UDHCPC6_RFC3646 || ENABLE_FEATURE_UDHCPC6_RFC4704 char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC; uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) FAST_FUNC; #endif @@ -284,7 +290,10 @@ void udhcp_dump_packet(struct dhcp_packet *packet) FAST_FUNC; /* 2nd param is "uint32_t*" */ int FAST_FUNC udhcp_str2nip(const char *str, void *arg); /* 2nd param is "struct option_set**" */ -int FAST_FUNC udhcp_str2optset(const char *str, void *arg); +int FAST_FUNC udhcp_str2optset(const char *str, + void *arg, + const struct dhcp_optflag *optflags, + const char *option_strings); void udhcp_init_header(struct dhcp_packet *packet, char type) FAST_FUNC; diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index ef9b9a5f2..f6d3fb98b 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c @@ -2,26 +2,49 @@ /* * DHCPv6 client. * - * 2011-11. - * WARNING: THIS CODE IS INCOMPLETE. IT IS NOWHERE NEAR - * TO BE READY FOR PRODUCTION USE. + * WARNING: THIS CODE IS INCOMPLETE. * - * Copyright (C) 2011 Denys Vlasenko. + * Copyright (C) 2011-2017 Denys Vlasenko. * * Licensed under GPLv2, see file LICENSE in this source tree. */ //config:config UDHCPC6 -//config: bool "udhcpc6 (DHCPv6 client, NOT READY)" +//config: bool "udhcpc6 (DHCPv6 client, EXPERIMENTAL)" //config: default n # not yet ready //config: depends on FEATURE_IPV6 //config: help //config: udhcpc6 is a DHCPv6 client +//config: +//config:config FEATURE_UDHCPC6_RFC3646 +//config: bool "Support RFC 3646 (DNS server and search list)" +//config: default y +//config: depends on UDHCPC6 +//config: help +//config: List of DNS servers and domain search list can be requested with +//config: "-O dns" and "-O search". If server gives these values, +//config: they will be set in environment variables "dns" and "search". +//config: +//config:config FEATURE_UDHCPC6_RFC4704 +//config: bool "Support RFC 4704 (Client FQDN)" +//config: default y +//config: depends on UDHCPC6 +//config: help +//config: You can request FQDN to be given by server using "-O fqdn". +//config: +//config:config FEATURE_UDHCPC6_RFC4833 +//config: bool "Support RFC 4833 (Timezones)" +//config: default y +//config: depends on UDHCPC6 +//config: help +//config: You can request POSIX timezone with "-O tz" and timezone name +//config: with "-O timezone". //applet:IF_UDHCPC6(APPLET(udhcpc6, BB_DIR_USR_BIN, BB_SUID_DROP)) //kbuild:lib-$(CONFIG_UDHCPC6) += d6_dhcpc.o d6_packet.o d6_socket.o common.o socket.o signalpipe.o - +//kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC3646) += domain_codec.o +//kbuild:lib-$(CONFIG_FEATURE_UDHCPC6_RFC4704) += domain_codec.o #include /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ @@ -37,6 +60,34 @@ /* "struct client_config_t client_config" is in bb_common_bufsiz1 */ +static const struct dhcp_optflag d6_optflags[] = { +#if ENABLE_FEATURE_UDHCPC6_RFC3646 + { OPTION_6RD | OPTION_LIST | OPTION_REQ, D6_OPT_DNS_SERVERS }, + { OPTION_DNS_STRING | OPTION_LIST | OPTION_REQ, D6_OPT_DOMAIN_LIST }, +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4704 + { OPTION_DNS_STRING, D6_OPT_CLIENT_FQDN }, +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4833 + { OPTION_STRING, D6_OPT_TZ_POSIX }, + { OPTION_STRING, D6_OPT_TZ_NAME }, +#endif + { 0, 0 } +}; +/* Must match d6_optflags[] order */ +static const char d6_option_strings[] ALIGN1 = +#if ENABLE_FEATURE_UDHCPC6_RFC3646 + "dns" "\0" /* D6_OPT_DNS_SERVERS */ + "search" "\0" /* D6_OPT_DOMAIN_LIST */ +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4704 + "fqdn" "\0" /* D6_OPT_CLIENT_FQDN */ +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4833 + "tz" "\0" /* D6_OPT_TZ_POSIX */ + "timezone" "\0" /* D6_OPT_TZ_NAME */ +#endif + "\0"; #if ENABLE_LONG_OPTS static const char udhcpc6_longopts[] ALIGN1 = @@ -88,14 +139,7 @@ enum { IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) }; -static const char opt_req[] = { - (D6_OPT_ORO >> 8), (D6_OPT_ORO & 0xff), - 0, 6, - (D6_OPT_DNS_SERVERS >> 8), (D6_OPT_DNS_SERVERS & 0xff), - (D6_OPT_DOMAIN_LIST >> 8), (D6_OPT_DOMAIN_LIST & 0xff), - (D6_OPT_CLIENT_FQDN >> 8), (D6_OPT_CLIENT_FQDN & 0xff), -}; - +#if ENABLE_FEATURE_UDHCPC6_RFC4704 static const char opt_fqdn_req[] = { (D6_OPT_CLIENT_FQDN >> 8), (D6_OPT_CLIENT_FQDN & 0xff), 0, 2, /* optlen */ @@ -105,6 +149,7 @@ static const char opt_fqdn_req[] = { /* N=0: server SHOULD perform updates (PTR RR only in our case, since S=0) */ 0 /* empty DNS-encoded name */ }; +#endif /*** Utility functions ***/ @@ -152,10 +197,12 @@ static char** new_env(void) /* put all the parameters into the environment */ static void option_to_env(uint8_t *option, uint8_t *option_end) { - char *dlist; +#if ENABLE_FEATURE_UDHCPC6_RFC3646 + int addrs, option_offset; +#endif /* "length minus 4" */ int len_m4 = option_end - option - 4; - int addrs, option_offset; + while (len_m4 >= 0) { uint32_t v32; char ipv6str[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")]; @@ -237,7 +284,10 @@ static void option_to_env(uint8_t *option, uint8_t *option_end) sprint_nip6(ipv6str, option + 4 + 4 + 1); *new_env() = xasprintf("ipv6prefix=%s/%u", ipv6str, (unsigned)(option[4 + 4])); break; - case D6_OPT_DNS_SERVERS: +#if ENABLE_FEATURE_UDHCPC6_RFC3646 + case D6_OPT_DNS_SERVERS: { + char *dlist; + /* Make sure payload-size is a multiple of 16 */ if ((option[3] & 0x0f) != 0) break; @@ -259,13 +309,21 @@ static void option_to_env(uint8_t *option, uint8_t *option_end) } break; - case D6_OPT_DOMAIN_LIST: + } + case D6_OPT_DOMAIN_LIST: { + char *dlist; + dlist = dname_dec(option + 4, (option[2] << 8) | option[3], "search="); if (!dlist) break; *new_env() = dlist; break; - case D6_OPT_CLIENT_FQDN: + } +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4704 + case D6_OPT_CLIENT_FQDN: { + char *dlist; + if (option[3] == 0) break; /* Work around broken ISC DHCPD6. @@ -284,6 +342,9 @@ static void option_to_env(uint8_t *option, uint8_t *option_end) break; *new_env() = dlist; break; + } +#endif +#if ENABLE_FEATURE_UDHCPC6_RFC4833 /* RFC 4833 Timezones */ case D6_OPT_TZ_POSIX: *new_env() = xasprintf("tz=%.*s", (int)option[3], (char*)option + 4); @@ -291,6 +352,7 @@ static void option_to_env(uint8_t *option, uint8_t *option_end) case D6_OPT_TZ_NAME: *new_env() = xasprintf("tz_name=%.*s", (int)option[3], (char*)option + 4); break; +#endif } len_m4 -= 4 + option[3]; option += 4 + option[3]; @@ -363,17 +425,33 @@ static uint8_t *init_d6_packet(struct d6_packet *packet, char type, uint32_t xid static uint8_t *add_d6_client_options(uint8_t *ptr) { - return ptr; - //uint8_t c; - //int i, end, len; + uint8_t *start = ptr; + unsigned option; + + ptr += 4; + for (option = 1; option < 256; option++) { + if (client_config.opt_mask[option >> 3] & (1 << (option & 7))) { + ptr[0] = (option >> 8); + ptr[1] = option; + ptr += 2; + } + } - /* Add a "param req" option with the list of options we'd like to have - * from stubborn DHCP servers. Pull the data from the struct in common.c. - * No bounds checking because it goes towards the head of the packet. */ - //... + if ((ptr - start - 4) != 0) { + start[0] = (D6_OPT_ORO >> 8); + start[1] = D6_OPT_ORO; + start[2] = ((ptr - start - 4) >> 8); + start[3] = (ptr - start - 4); + } else + ptr = start; +#if ENABLE_FEATURE_UDHCPC6_RFC4704 + ptr = mempcpy(ptr, &opt_fqdn_req, sizeof(opt_fqdn_req)); +#endif /* Add -x options if any */ //... + + return ptr; } static int d6_mcast_from_client_config_ifindex(struct d6_packet *packet, uint8_t *end) @@ -497,10 +575,6 @@ static NOINLINE int send_d6_discover(uint32_t xid, struct in6_addr *requested_ip } opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, len); - /* Request additional options */ - opt_ptr = mempcpy(opt_ptr, &opt_req, sizeof(opt_req)); - opt_ptr = mempcpy(opt_ptr, &opt_fqdn_req, sizeof(opt_fqdn_req)); - /* Add options: * "param req" option according to -O, options specified with -x */ @@ -554,10 +628,6 @@ static NOINLINE int send_d6_select(uint32_t xid) /* IA NA (contains requested IP) */ opt_ptr = mempcpy(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2); - /* Request additional options */ - opt_ptr = mempcpy(opt_ptr, &opt_req, sizeof(opt_req)); - opt_ptr = mempcpy(opt_ptr, &opt_fqdn_req, sizeof(opt_fqdn_req)); - /* Add options: * "param req" option according to -O, options specified with -x */ @@ -1063,20 +1133,18 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) char *optstr = llist_pop(&list_O); unsigned n = bb_strtou(optstr, NULL, 0); if (errno || n > 254) { - n = udhcp_option_idx(optstr); - n = dhcp_optflags[n].code; + n = udhcp_option_idx(optstr, d6_option_strings); + n = d6_optflags[n].code; } client_config.opt_mask[n >> 3] |= 1 << (n & 7); } if (!(opt & OPT_o)) { - /* unsigned i, n; - for (i = 0; (n = dhcp_optflags[i].code) != 0; i++) { - if (dhcp_optflags[i].flags & OPTION_REQ) { + for (i = 0; (n = d6_optflags[i].code) != 0; i++) { + if (d6_optflags[i].flags & OPTION_REQ) { client_config.opt_mask[n >> 3] |= 1 << (n & 7); } } - */ } while (list_x) { char *optstr = llist_pop(&list_x); @@ -1085,7 +1153,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) *colon = ' '; /* now it looks similar to udhcpd's config file line: * "optname optval", using the common routine: */ - udhcp_str2optset(optstr, &client_config.options); + udhcp_str2optset(optstr, &client_config.options, d6_optflags, d6_option_strings); if (colon) *colon = ':'; /* restore it for NOMMU reexec */ } diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 6aa6731fb..1a66c610e 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -1346,7 +1346,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) char *optstr = llist_pop(&list_O); unsigned n = bb_strtou(optstr, NULL, 0); if (errno || n > 254) { - n = udhcp_option_idx(optstr); + n = udhcp_option_idx(optstr, dhcp_option_strings); n = dhcp_optflags[n].code; } client_config.opt_mask[n >> 3] |= 1 << (n & 7); @@ -1366,7 +1366,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) *colon = ' '; /* now it looks similar to udhcpd's config file line: * "optname optval", using the common routine: */ - udhcp_str2optset(optstr, &client_config.options); + udhcp_str2optset(optstr, &client_config.options, dhcp_optflags, dhcp_option_strings); if (colon) *colon = ':'; /* restore it for NOMMU reexec */ } diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h index 9f423a5b2..7fdbc9a6c 100644 --- a/networking/udhcp/dhcpc.h +++ b/networking/udhcp/dhcpc.h @@ -12,6 +12,7 @@ struct client_config_t { IF_FEATURE_UDHCP_PORT(uint16_t port;) int ifindex; /* Index number of the interface to use */ uint8_t opt_mask[256 / 8]; /* Bitmask of options to send (-O option) */ +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TODO: DHCPv6 has 16-bit option numbers const char *interface; /* The name of the interface to use */ char *pidfile; /* Optionally store the process ID */ const char *script; /* User script to run at dhcp events */ diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 5eff026bc..3a5fc2db7 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -361,6 +361,10 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg) return 1; } +static int FAST_FUNC read_optset(const char *line, void *arg) { + return udhcp_str2optset(line, arg, dhcp_optflags, dhcp_option_strings); +} + struct config_keyword { const char *keyword; int (*handler)(const char *line, void *var) FAST_FUNC; @@ -387,8 +391,8 @@ static const struct config_keyword keywords[] = { {"pidfile" , read_str , OFS(pidfile ), "/var/run/udhcpd.pid"}, {"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"}, /* keywords with no defaults must be last! */ - {"option" , udhcp_str2optset, OFS(options ), ""}, - {"opt" , udhcp_str2optset, OFS(options ), ""}, + {"option" , read_optset , OFS(options ), ""}, + {"opt" , read_optset , OFS(options ), ""}, {"notify_file" , read_str , OFS(notify_file ), NULL}, {"sname" , read_str , OFS(sname ), NULL}, {"boot_file" , read_str , OFS(boot_file ), NULL},