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).
"msstaticroutes""\0"/* DHCP_MS_STATIC_ROUTES */
"wpad" "\0" /* DHCP_WPAD */
;
+#endif
/* Lengths of the option types in binary form.
* Used by:
# 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);
}
}
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)
{
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)
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;
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;
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
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
/* 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;
/*
* 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 <syslog.h>
/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
/* "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 =
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 */
/* N=0: server SHOULD perform updates (PTR RR only in our case, since S=0) */
0 /* empty DNS-encoded name */
};
+#endif
/*** Utility functions ***/
/* 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")];
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;
}
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.
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);
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];
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)
}
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
*/
/* 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
*/
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);
*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 */
}