Blame SOURCES/0012-RFC-3442-Classless-Static-Route-Option-for-DHCPv4-51.patch

f9ed25
From 01b1dcfef129a4eccfaf0f63a216774019f82dca Mon Sep 17 00:00:00 2001
f9ed25
From: Pavel Zhukov <pzhukov@redhat.com>
f9ed25
Date: Thu, 21 Feb 2019 10:32:35 +0100
f9ed25
Subject: [PATCH 12/26] RFC 3442 - Classless Static Route Option for DHCPv4
f9ed25
 (#516325)
f9ed25
Cc: pzhukov@redhat.com
f9ed25
f9ed25
(Submitted to dhcp-bugs@isc.org - [ISC-Bugs #24572])
f9ed25
---
f9ed25
 client/clparse.c      | 13 ++++++++++--
f9ed25
 common/dhcp-options.5 | 43 +++++++++++++++++++++++++++++++++++++++
f9ed25
 common/inet.c         | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
f9ed25
 common/options.c      | 49 +++++++++++++++++++++++++++++++++++++++++++-
f9ed25
 common/parse.c        | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++-
f9ed25
 common/tables.c       |  2 ++
f9ed25
 includes/dhcp.h       |  1 +
f9ed25
 includes/dhcpd.h      |  2 ++
f9ed25
 includes/dhctoken.h   |  5 +++--
f9ed25
 9 files changed, 219 insertions(+), 6 deletions(-)
f9ed25
f9ed25
diff --git a/client/clparse.c b/client/clparse.c
f9ed25
index 44387ed..862e4f9 100644
f9ed25
--- a/client/clparse.c
f9ed25
+++ b/client/clparse.c
f9ed25
@@ -31,7 +31,7 @@
f9ed25
 
f9ed25
 struct client_config top_level_config;
f9ed25
 
f9ed25
-#define NUM_DEFAULT_REQUESTED_OPTS	14
f9ed25
+#define NUM_DEFAULT_REQUESTED_OPTS	15
f9ed25
 /* There can be 2 extra requested options for DHCPv4-over-DHCPv6. */
f9ed25
 struct option *default_requested_options[NUM_DEFAULT_REQUESTED_OPTS + 2 + 1];
f9ed25
 
f9ed25
@@ -87,7 +87,11 @@ isc_result_t read_client_conf ()
f9ed25
 				dhcp_universe.code_hash, &code, 0, MDL);
f9ed25
 
f9ed25
 	/* 4 */
f9ed25
-	code = DHO_ROUTERS;
f9ed25
+	/* The Classless Static Routes option code MUST appear in the parameter
f9ed25
+     * request list prior to both the Router option code and the Static
f9ed25
+     * Routes option code, if present. (RFC3442)
f9ed25
+	 */
f9ed25
+	code = DHO_CLASSLESS_STATIC_ROUTES;
f9ed25
 	option_code_hash_lookup(&default_requested_options[3],
f9ed25
 				dhcp_universe.code_hash, &code, 0, MDL);
f9ed25
 
f9ed25
@@ -141,6 +145,11 @@ isc_result_t read_client_conf ()
f9ed25
 	option_code_hash_lookup(&default_requested_options[13],
f9ed25
 				dhcp_universe.code_hash, &code, 0, MDL);
f9ed25
 
f9ed25
+	/* 15 */
f9ed25
+	code = DHO_ROUTERS;
f9ed25
+	option_code_hash_lookup(&default_requested_options[14],
f9ed25
+				dhcp_universe.code_hash, &code, 0, MDL);
f9ed25
+
f9ed25
 	for (code = 0 ; code < NUM_DEFAULT_REQUESTED_OPTS ; code++) {
f9ed25
 		if (default_requested_options[code] == NULL)
f9ed25
 			log_fatal("Unable to find option definition for "
f9ed25
diff --git a/common/dhcp-options.5 b/common/dhcp-options.5
f9ed25
index d9e1197..2343b19 100644
f9ed25
--- a/common/dhcp-options.5
f9ed25
+++ b/common/dhcp-options.5
f9ed25
@@ -110,6 +110,26 @@ hexadecimal, separated by colons.  For example:
f9ed25
 or
f9ed25
   option dhcp-client-identifier 43:4c:49:45:54:2d:46:4f:4f;
f9ed25
 .fi
f9ed25
+.PP
f9ed25
+The
f9ed25
+.B destination-descriptor
f9ed25
+describe the IP subnet number and subnet mask
f9ed25
+of a particular destination using a compact encoding. This encoding
f9ed25
+consists of one octet describing the width of the subnet mask,
f9ed25
+followed by all the significant octets of the subnet number.
f9ed25
+The following table contains some examples of how various subnet
f9ed25
+number/mask combinations can be encoded:
f9ed25
+.nf
f9ed25
+.sp 1
f9ed25
+Subnet number   Subnet mask      Destination descriptor
f9ed25
+0               0                0
f9ed25
+10.0.0.0        255.0.0.0        8.10
f9ed25
+10.0.0.0        255.255.255.0    24.10.0.0
f9ed25
+10.17.0.0       255.255.0.0      16.10.17
f9ed25
+10.27.129.0     255.255.255.0    24.10.27.129
f9ed25
+10.229.0.128    255.255.255.128  25.10.229.0.128
f9ed25
+10.198.122.47   255.255.255.255  32.10.198.122.47
f9ed25
+.fi
f9ed25
 .SH SETTING OPTION VALUES USING EXPRESSIONS
f9ed25
 Sometimes it's helpful to be able to set the value of a DHCP option
f9ed25
 based on some value that the client has sent.  To do this, you can
f9ed25
@@ -1086,6 +1106,29 @@ dhclient-script will create routes:
f9ed25
 .RE
f9ed25
 .PP
f9ed25
 .nf
f9ed25
+.B option \fBclassless-static-routes\fR \fIdestination-descriptor ip-address\fR
f9ed25
+                            [\fB,\fR \fIdestination-descriptor ip-address\fR...]\fB;\fR
f9ed25
+.fi
f9ed25
+.RS 0.25i
f9ed25
+.PP
f9ed25
+This option (see RFC3442) specifies a list of classless static routes
f9ed25
+that the client should install in its routing cache.
f9ed25
+.PP
f9ed25
+This option can contain one or more static routes, each of which
f9ed25
+consists of a destination descriptor and the IP address of the router
f9ed25
+that should be used to reach that destination.
f9ed25
+.PP
f9ed25
+Many clients may not implement the Classless Static Routes option.
f9ed25
+DHCP server administrators should therefore configure their DHCP
f9ed25
+servers to send both a Router option and a Classless Static Routes
f9ed25
+option, and should specify the default router(s) both in the Router
f9ed25
+option and in the Classless Static Routes option.
f9ed25
+.PP
f9ed25
+If the DHCP server returns both a Classless Static Routes option and
f9ed25
+a Router option, the DHCP client ignores the Router option.
f9ed25
+.RE
f9ed25
+.PP
f9ed25
+.nf
f9ed25
 .B option \fBstreettalk-directory-assistance-server\fR \fIip-address\fR
f9ed25
                                            [\fB,\fR \fIip-address\fR...]\fB;\fR
f9ed25
 .fi
f9ed25
diff --git a/common/inet.c b/common/inet.c
f9ed25
index c4da73c..981fb92 100644
f9ed25
--- a/common/inet.c
f9ed25
+++ b/common/inet.c
f9ed25
@@ -519,6 +519,60 @@ free_iaddrcidrnetlist(struct iaddrcidrnetlist **result) {
f9ed25
 	return ISC_R_SUCCESS;
f9ed25
 }
f9ed25
 
f9ed25
+static const char *
f9ed25
+inet_ntopdd(const unsigned char *src, unsigned srclen, char *dst, size_t size)
f9ed25
+{
f9ed25
+	char tmp[sizeof("32.255.255.255.255")];
f9ed25
+	int len;
f9ed25
+
f9ed25
+	switch (srclen) {
f9ed25
+		case 2:
f9ed25
+			len = sprintf (tmp, "%u.%u", src[0], src[1]);
f9ed25
+			break;
f9ed25
+		case 3:
f9ed25
+			len = sprintf (tmp, "%u.%u.%u", src[0], src[1], src[2]);
f9ed25
+			break;
f9ed25
+		case 4:
f9ed25
+			len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
f9ed25
+			break;
f9ed25
+		case 5:
f9ed25
+			len = sprintf (tmp, "%u.%u.%u.%u.%u", src[0], src[1], src[2], src[3], src[4]);
f9ed25
+			break;
f9ed25
+		default:
f9ed25
+			return NULL;
f9ed25
+	}
f9ed25
+	if (len < 0)
f9ed25
+		return NULL;
f9ed25
+
f9ed25
+	if (len > size) {
f9ed25
+		errno = ENOSPC;
f9ed25
+		return NULL;
f9ed25
+	}
f9ed25
+
f9ed25
+	return strcpy (dst, tmp);
f9ed25
+}
f9ed25
+
f9ed25
+/* pdestdesc() turns an iaddr structure into a printable dest. descriptor */
f9ed25
+const char *
f9ed25
+pdestdesc(const struct iaddr addr) {
f9ed25
+	static char pbuf[sizeof("255.255.255.255.255")];
f9ed25
+
f9ed25
+	if (addr.len == 0) {
f9ed25
+		return "<null destination descriptor>";
f9ed25
+	}
f9ed25
+	if (addr.len == 1) {
f9ed25
+		return "0";
f9ed25
+	}
f9ed25
+	if ((addr.len >= 2) && (addr.len <= 5)) {
f9ed25
+		return inet_ntopdd(addr.iabuf, addr.len, pbuf, sizeof(pbuf));
f9ed25
+	}
f9ed25
+
f9ed25
+	log_fatal("pdestdesc():%s:%d: Invalid destination descriptor length %d.",
f9ed25
+		  MDL, addr.len);
f9ed25
+	/* quell compiler warnings */
f9ed25
+	return NULL;
f9ed25
+}
f9ed25
+
f9ed25
 /* piaddr() turns an iaddr structure into a printable address. */
f9ed25
 /* XXX: should use a const pointer rather than passing the structure */
f9ed25
 const char *
f9ed25
diff --git a/common/options.c b/common/options.c
f9ed25
index fc0e088..3034cf0 100644
f9ed25
--- a/common/options.c
f9ed25
+++ b/common/options.c
f9ed25
@@ -729,7 +729,11 @@ cons_options(struct packet *inpacket, struct dhcp_packet *outpacket,
f9ed25
 		 * packet.
f9ed25
 		 */
f9ed25
 		priority_list[priority_len++] = DHO_SUBNET_MASK;
f9ed25
-		priority_list[priority_len++] = DHO_ROUTERS;
f9ed25
+		if (lookup_option(&dhcp_universe, cfg_options,
f9ed25
+							DHO_CLASSLESS_STATIC_ROUTES))
f9ed25
+			priority_list[priority_len++] = DHO_CLASSLESS_STATIC_ROUTES;
f9ed25
+		else
f9ed25
+			priority_list[priority_len++] = DHO_ROUTERS;
f9ed25
 		priority_list[priority_len++] = DHO_DOMAIN_NAME_SERVERS;
f9ed25
 		priority_list[priority_len++] = DHO_HOST_NAME;
f9ed25
 		priority_list[priority_len++] = DHO_FQDN;
f9ed25
@@ -1804,6 +1808,7 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
f9ed25
 	unsigned long tval;
f9ed25
 	isc_boolean_t a_array = ISC_FALSE;
f9ed25
 	int len_used;
f9ed25
+	unsigned int octets = 0;
f9ed25
 
f9ed25
 	if (emit_commas)
f9ed25
 		comma = ',';
f9ed25
@@ -1812,6 +1817,7 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
f9ed25
 
f9ed25
 	memset (enumbuf, 0, sizeof enumbuf);
f9ed25
 
f9ed25
+	if (option->format[0] != 'R') { /* see explanation lower */
f9ed25
 	/* Figure out the size of the data. */
f9ed25
 	for (l = i = 0; option -> format [i]; i++, l++) {
f9ed25
 		if (l >= sizeof(fmtbuf) - 1)
f9ed25
@@ -2004,6 +2010,33 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
f9ed25
 	if (numhunk < 0)
f9ed25
 		numhunk = 1;
f9ed25
 
f9ed25
+	} else { /* option->format[i] == 'R') */
f9ed25
+		/* R (destination descriptor) has variable length.
f9ed25
+		 * We can find it only in classless static route option,
f9ed25
+		 * so we are for sure parsing classless static route option now.
f9ed25
+		 * We go through whole the option to check whether there are no
f9ed25
+		 * missing/extra bytes.
f9ed25
+		 * I didn't find out how to improve the existing code and that's the
f9ed25
+		 * reason for this separate 'else' where I do my own checkings.
f9ed25
+		 * I know it's little bit unsystematic, but it works.
f9ed25
+		 */
f9ed25
+		numhunk = 0;
f9ed25
+		numelem = 2; /* RI */
f9ed25
+		fmtbuf[0]='R'; fmtbuf[1]='I'; fmtbuf[2]=0;
f9ed25
+		for (i =0; i < len; i = i + octets + 5) {
f9ed25
+			if (data[i] > 32) { /* subnet mask width */
f9ed25
+				log_error ("wrong subnet mask width in destination descriptor");
f9ed25
+				break;
f9ed25
+			}
f9ed25
+			numhunk++;
f9ed25
+			octets = ((data[i]+7) / 8);
f9ed25
+		}
f9ed25
+		if (i != len) {
f9ed25
+			log_error ("classless static routes option has wrong size or "
f9ed25
+					   "there's some garbage in format");
f9ed25
+		}
f9ed25
+	}
f9ed25
+
f9ed25
 	/* Cycle through the array (or hunk) printing the data. */
f9ed25
 	for (i = 0; i < numhunk; i++) {
f9ed25
 		if ((a_array == ISC_TRUE) && (i != 0) && (numelem > 0)) {
f9ed25
@@ -2159,6 +2192,20 @@ const char *pretty_print_option (option, data, len, emit_commas, emit_quotes)
f9ed25
 				strcpy(op, piaddr(iaddr));
f9ed25
 				dp += 4;
f9ed25
 				break;
f9ed25
+
f9ed25
+			      case 'R':
f9ed25
+				if (dp[0] <= 32)
f9ed25
+					iaddr.len = (((dp[0]+7)/8)+1);
f9ed25
+				else {
f9ed25
+					log_error ("wrong subnet mask width in destination descriptor");
f9ed25
+					return "<error>";
f9ed25
+				}
f9ed25
+
f9ed25
+				memcpy(iaddr.iabuf, dp, iaddr.len);
f9ed25
+				strcpy(op, pdestdesc(iaddr));
f9ed25
+				dp += iaddr.len;
f9ed25
+				break;
f9ed25
+
f9ed25
 			      case '6':
f9ed25
 				iaddr.len = 16;
f9ed25
 				memcpy(iaddr.iabuf, dp, 16);
f9ed25
diff --git a/common/parse.c b/common/parse.c
f9ed25
index 3ac4ebf..f17bc0b 100644
f9ed25
--- a/common/parse.c
f9ed25
+++ b/common/parse.c
f9ed25
@@ -344,6 +344,39 @@ int parse_ip_addr (cfile, addr)
f9ed25
 	return 0;
f9ed25
 }	
f9ed25
 
f9ed25
+/*
f9ed25
+ * destination-descriptor :== NUMBER DOT NUMBER |
f9ed25
+ *                            NUMBER DOT NUMBER DOT NUMBER |
f9ed25
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER |
f9ed25
+ *                            NUMBER DOT NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
f9ed25
+ */
f9ed25
+
f9ed25
+int parse_destination_descriptor (cfile, addr)
f9ed25
+	struct parse *cfile;
f9ed25
+	struct iaddr *addr;
f9ed25
+{
f9ed25
+		unsigned int mask_width, dest_dest_len;
f9ed25
+		addr -> len = 0;
f9ed25
+		if (parse_numeric_aggregate (cfile, addr -> iabuf,
f9ed25
+									 &addr -> len, DOT, 10, 8)) {
f9ed25
+			mask_width = (unsigned int)addr->iabuf[0];
f9ed25
+			dest_dest_len = (((mask_width+7)/8)+1);
f9ed25
+			if (mask_width > 32) {
f9ed25
+				parse_warn (cfile,
f9ed25
+				"subnet mask width (%u) greater than 32.", mask_width);
f9ed25
+			}
f9ed25
+			else if (dest_dest_len != addr->len) {
f9ed25
+				parse_warn (cfile,
f9ed25
+				"destination descriptor with subnet mask width %u "
f9ed25
+				"should have %u octets, but has %u octets.",
f9ed25
+				mask_width, dest_dest_len, addr->len);
f9ed25
+			}
f9ed25
+
f9ed25
+			return 1;
f9ed25
+		}
f9ed25
+		return 0;
f9ed25
+}
f9ed25
+
f9ed25
 /*
f9ed25
  * Return true if every character in the string is hexadecimal.
f9ed25
  */
f9ed25
@@ -724,8 +757,10 @@ unsigned char *parse_numeric_aggregate (cfile, buf,
f9ed25
 		if (count) {
f9ed25
 			token = peek_token (&val, (unsigned *)0, cfile);
f9ed25
 			if (token != separator) {
f9ed25
-				if (!*max)
f9ed25
+				if (!*max) {
f9ed25
+					*max = count;
f9ed25
 					break;
f9ed25
+				}
f9ed25
 				if (token != RBRACE && token != LBRACE)
f9ed25
 					token = next_token (&val,
f9ed25
 							    (unsigned *)0,
f9ed25
@@ -1672,6 +1707,9 @@ int parse_option_code_definition (cfile, option)
f9ed25
 	      case IP_ADDRESS:
f9ed25
 		type = 'I';
f9ed25
 		break;
f9ed25
+	      case DESTINATION_DESCRIPTOR:
f9ed25
+		type = 'R';
f9ed25
+		break;
f9ed25
 	      case IP6_ADDRESS:
f9ed25
 		type = '6';
f9ed25
 		break;
f9ed25
@@ -5101,6 +5139,15 @@ int parse_option_token (rv, cfile, fmt, expr, uniform, lookups)
f9ed25
 		}
f9ed25
 		break;
f9ed25
 
f9ed25
+	      case 'R': /* destination descriptor */
f9ed25
+		if (!parse_destination_descriptor (cfile, &addr)) {
f9ed25
+			return 0;
f9ed25
+		}
f9ed25
+		if (!make_const_data (&t, addr.iabuf, addr.len, 0, 1, MDL)) {
f9ed25
+			return 0;
f9ed25
+		}
f9ed25
+		break;
f9ed25
+
f9ed25
 	      case '6': /* IPv6 address. */
f9ed25
 		if (!parse_ip6_addr(cfile, &addr)) {
f9ed25
 			return 0;
f9ed25
@@ -5378,6 +5425,13 @@ int parse_option_decl (oc, cfile)
f9ed25
 					goto exit;
f9ed25
 				len = ip_addr.len;
f9ed25
 				dp = ip_addr.iabuf;
f9ed25
+				goto alloc;
f9ed25
+
f9ed25
+			      case 'R': /* destination descriptor */
f9ed25
+				if (!parse_destination_descriptor (cfile, &ip_addr))
f9ed25
+					goto exit;
f9ed25
+				len = ip_addr.len;
f9ed25
+				dp = ip_addr.iabuf;
f9ed25
 
f9ed25
 			      alloc:
f9ed25
 				if (hunkix + len > sizeof hunkbuf) {
f9ed25
diff --git a/common/tables.c b/common/tables.c
f9ed25
index d2294c0..f1be07d 100644
f9ed25
--- a/common/tables.c
f9ed25
+++ b/common/tables.c
f9ed25
@@ -45,6 +45,7 @@ HASH_FUNCTIONS (option_code, const unsigned *, struct option,
f9ed25
    Format codes:
f9ed25
 
f9ed25
    I - IPv4 address
f9ed25
+   R - destination descriptor (RFC3442)
f9ed25
    6 - IPv6 address
f9ed25
    l - 32-bit signed integer
f9ed25
    L - 32-bit unsigned integer
f9ed25
@@ -216,6 +217,7 @@ static struct option dhcp_options[] = {
f9ed25
 #endif
f9ed25
 	{ "subnet-selection", "I",		&dhcp_universe, 118, 1 },
f9ed25
 	{ "domain-search", "D",			&dhcp_universe, 119, 1 },
f9ed25
+	{ "classless-static-routes", "RIA",	&dhcp_universe, 121, 1 },
f9ed25
 	{ "vivco", "Evendor-class.",		&dhcp_universe, 124, 1 },
f9ed25
 	{ "vivso", "Evendor.",			&dhcp_universe, 125, 1 },
f9ed25
 #if 0
f9ed25
diff --git a/includes/dhcp.h b/includes/dhcp.h
f9ed25
index 0a74137..95bf539 100644
f9ed25
--- a/includes/dhcp.h
f9ed25
+++ b/includes/dhcp.h
f9ed25
@@ -158,6 +158,7 @@ struct dhcp_packet {
f9ed25
 #define DHO_ASSOCIATED_IP			92
f9ed25
 #define DHO_SUBNET_SELECTION			118 /* RFC3011! */
f9ed25
 #define DHO_DOMAIN_SEARCH			119 /* RFC3397 */
f9ed25
+#define DHO_CLASSLESS_STATIC_ROUTES		121 /* RFC3442 */
f9ed25
 #define DHO_VIVCO_SUBOPTIONS			124
f9ed25
 #define DHO_VIVSO_SUBOPTIONS			125
f9ed25
 
f9ed25
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
f9ed25
index 3632a6b..2ac39ae 100644
f9ed25
--- a/includes/dhcpd.h
f9ed25
+++ b/includes/dhcpd.h
f9ed25
@@ -2951,6 +2951,7 @@ isc_result_t range2cidr(struct iaddrcidrnetlist **result,
f9ed25
 			const struct iaddr *lo, const struct iaddr *hi);
f9ed25
 isc_result_t free_iaddrcidrnetlist(struct iaddrcidrnetlist **result);
f9ed25
 const char *piaddr (struct iaddr);
f9ed25
+const char *pdestdesc (struct iaddr);
f9ed25
 char *piaddrmask(struct iaddr *, struct iaddr *);
f9ed25
 char *piaddrcidr(const struct iaddr *, unsigned int);
f9ed25
 u_int16_t validate_port(char *);
f9ed25
@@ -3169,6 +3170,7 @@ void parse_client_lease_declaration (struct parse *,
f9ed25
 int parse_option_decl (struct option_cache **, struct parse *);
f9ed25
 void parse_string_list (struct parse *, struct string_list **, int);
f9ed25
 int parse_ip_addr (struct parse *, struct iaddr *);
f9ed25
+int parse_destination_descriptor (struct parse *, struct iaddr *);
f9ed25
 int parse_ip_addr_with_subnet(struct parse *, struct iaddrmatch *);
f9ed25
 void parse_reject_statement (struct parse *, struct client_config *);
f9ed25
 
f9ed25
diff --git a/includes/dhctoken.h b/includes/dhctoken.h
f9ed25
index 7e7215a..b4d93ba 100644
f9ed25
--- a/includes/dhctoken.h
f9ed25
+++ b/includes/dhctoken.h
f9ed25
@@ -376,8 +376,9 @@ enum dhcp_token {
f9ed25
 	LEASE_ID_FORMAT = 676,
f9ed25
 	TOKEN_HEX = 677,
f9ed25
 	TOKEN_OCTAL = 678,
f9ed25
-	KEY_ALGORITHM = 679
f9ed25
-        BOOTP_BROADCAST_ALWAYS = 680
f9ed25
+	KEY_ALGORITHM = 679,
f9ed25
+        BOOTP_BROADCAST_ALWAYS = 680,
f9ed25
+	DESTINATION_DESCRIPTOR = 681
f9ed25
 };
f9ed25
 
f9ed25
 #define is_identifier(x)	((x) >= FIRST_TOKEN &&	\
f9ed25
-- 
f9ed25
2.14.5
f9ed25