From ba4fbca8a81d765f81aefc74db7f73ec9ded3550 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 28 Jun 2017 19:18:17 +0200 Subject: udhcpc6: make -O OPT work Patch is based on work by tiggerswelt.net. Signed-off-by: Denys Vlasenko --- networking/udhcp/d6_dhcpc.c | 150 ++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 41 deletions(-) (limited to 'networking/udhcp/d6_dhcpc.c') 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 */ } -- cgit v1.2.3