[PATCH] Added support for timezones on dhcp6-client (RFC 4833)

tiggersWelt.net (Support) support at tiggerswelt.net
Tue Jun 27 23:53:11 UTC 2017


Am 27.06.2017 um 18:13 schrieb Denys Vlasenko:
[...]
> Let's unbreak -O OPT instead? IPv4 client adds list of -O options
> to outgoing packets using add_client_options()
[...]
> Can you try making a patch
@Denys: Please apologize for the double-post I missed to copy
the mailinglist in.

Here is my first proposal for this.

The option-handling stuff was somewhat tied to DHCPv4 and not
compatible with DHCPv6, so I've added additional parameters to
udhcp_option_idx() and udhcp_str2optset() - not that beautiful
but the goal was to reuse the code here.

For DHCPv6 I added a separate list of option-names and
flags and made each option to be selected at compile-time.
udhcpc6 also had a dependency on domain_codec.o that was not
met if busybox was built without udhcpc was built without RFC
3397-Support.

opt_req[] has gone away as requested. Instead I use opt_mask to
create the Option-Request-Option. It has to be noticed that D6-
Option-Numbers are 16-Bit wide and don't fit into opt_mask. At
the moment this should not be an issue as the highest assigned
number is 143. Maybe at some time in future this could become
a problem...

---
 networking/udhcp/common.c   |  20 ++++---
 networking/udhcp/common.h   |  15 ++++--
 networking/udhcp/d6_dhcpc.c | 123 +++++++++++++++++++++++++++++++++-----------
 networking/udhcp/dhcpc.c    |   4 +-
 networking/udhcp/dhcpc.h    |   1 +
 networking/udhcp/dhcpd.c    |   8 ++-
 6 files changed, 125 insertions(+), 46 deletions(-)

diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c
index 420695a20..142d863bf 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).
@@ -137,6 +138,9 @@ const char dhcp_option_strings[] ALIGN1 =
 	"wpad" "\0"        /* DHCP_WPAD           */
 	;

+const size_t dhcp_option_size = sizeof(dhcp_option_strings);
+#endif
+
 /* Lengths of the option types in binary form.
  * Used by:
  * udhcp_str2optset: to determine how many bytes to allocate.
@@ -190,17 +194,17 @@ 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 *options, size_t size)
 {
-	int n = index_in_strings(dhcp_option_strings, name);
+	int n = index_in_strings(options, name);
 	if (n >= 0)
 		return n;

 	{
-		char buf[sizeof(dhcp_option_strings)];
+		char buf[size];
 		char *d = buf;
-		const char *s = dhcp_option_strings;
-		while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) {
+		const char *s = options;
+		while (s < options + size - 2) {
 			*d++ = (*s == '\0' ? ' ' : *s);
 			s++;
 		}
@@ -315,6 +319,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 +343,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 +457,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 *options, size_t size)
 {
 	struct option_set **opt_list = arg;
 	char *opt, *val;
@@ -478,7 +484,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, options, size)];
 	}

 	retval = 0;
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
index ee12cf91b..f4ce39e2d 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,22 @@ 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;
+extern const size_t dhcp_option_size;
+#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 *options, size_t size);

 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 +291,7 @@ 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 *options, size_t size);

 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..d31d36d7d 100644
--- a/networking/udhcp/d6_dhcpc.c
+++ b/networking/udhcp/d6_dhcpc.c
@@ -17,11 +17,35 @@
 //config:	depends on FEATURE_IPV6
 //config:	help
 //config:	  udhcpc6 is a DHCPv6 client
+//config:
+//config:config FEATURE_UDHCPC6_RFC3646
+//config:	bool "Enable support for RFC 3646 (DNS-Server and Search-List)"
+//config:	default y
+//config:	depends on UDHCPC6
+//config:	help
+//config:	  Adds support for RFC 3646 on udhcpc6. List of DNS-Servers and
+//config:	  DNS-Searchlist may be requested from dhcpd6 and will be forwarded
+//config:	  to environment-variables "dns" and "search" of DHCP-Event-Script.
+//config:
+//config:config FEATURE_UDHCPC6_RFC4704
+//config:	bool "Enable support for RFC 4704 (Client FQDN)"
+//config:	default y
+//config:	depends on UDHCPC6
+//config:	help
+//config:	  Enables support for RFC 4704 to retrive an fqdn for the client.
+//config:
+//config:config FEATURE_UDHCPC6_RFC4833
+//config:	bool "Enable support for RFC 4833 (Timezones)"
+//config:	default y
+//config:	depends on UDHCPC6
+//config:	help
+//config:	  Allow to request timezone-definitions from dhcpd

 //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 */
@@ -37,6 +61,37 @@

 /* "struct client_config_t client_config" is in bb_common_bufsiz1 */

+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 }
+};
+
+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
+#if !ENABLE_FEATURE_UDHCPC6_RFC3646 && !ENABLE_FEATURE_UDHCPC6_RFC4704 && !ENABLE_FEATURE_UDHCPC6_RFC4833
+	"\0"
+#endif
+	;

 #if ENABLE_LONG_OPTS
 static const char udhcpc6_longopts[] ALIGN1 =
@@ -88,14 +143,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 +153,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 ***/

@@ -237,6 +286,7 @@ 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;
+#if ENABLE_FEATURE_UDHCPC6_RFC3646
 		case D6_OPT_DNS_SERVERS:
 			/* Make sure payload-size is a multiple of 16 */
 			if ((option[3] & 0x0f) != 0)
@@ -265,6 +315,8 @@ static void option_to_env(uint8_t *option, uint8_t *option_end)
 				break;
 			*new_env() = dlist;
 			break;
+#endif
+#if ENABLE_FEATURE_UDHCPC6_RFC4704
 		case D6_OPT_CLIENT_FQDN:
 			if (option[3] == 0)
 				break;
@@ -284,6 +336,8 @@ 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 +345,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,15 +418,31 @@ 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;
+	uint16_t option;

-	/* 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. */
-	//...
+	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;
+		}
+
+	if (ptr > start + 4) {
+		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
+
+	return ptr;
 	/* Add -x options if any */
 	//...
 }
@@ -497,10 +568,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 +621,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 +1126,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, sizeof(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 +1146,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, (const struct dhcp_optflag **)&d6_optflags, d6_option_strings, sizeof(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..3744ae3d9 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, dhcp_option_size);
 			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, (const struct dhcp_optflag **)&dhcp_optflags, dhcp_option_strings, dhcp_option_size);
 		if (colon)
 			*colon = ':'; /* restore it for NOMMU reexec */
 	}
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h
index 9f423a5b2..53545b062 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..6fd7a3ce8 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, (const struct dhcp_optflag **)&dhcp_optflags, dhcp_option_strings, dhcp_option_size);
+}
+
 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},
-- 
2.13.1

-- 
    \\\||///
  \\  - -  //
   (  @ @  )
-oOo--( )--oOo-------------------------------------------------------
 tiggersWelt.net                                 www.tiggersWelt.net
 Inhaber Bernd Holzmüller                       info at tiggerswelt.net
                                            Büro: 07 11 / 550 425-90
 Marktstraße 57                              Fax: 07 11 / 550 425-99
 70372 Stuttgart

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3310 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20170628/4487a1d7/attachment.p7s>


More information about the busybox mailing list